lexical scope - Type extensions and members visiblity in F# -


f# has feature called "type extension" gives developer ability extend existing types. there 2 types of extensions: intrinsic extension , optional extension. first 1 similar partial types in c# , second 1 similar method extension (but more powerful).

to use intrinsic extension should put 2 declarations same file. in case compiler merge 2 definitions 1 final type (i.e. 2 "parts" of 1 type).

the issue 2 types has different access rules different members , values:

// sampletype.fs // "main" declaration type sampletype(a: int) =     let f1 = 42     let func() = 42      [<defaultvalue>]     val mutable f2: int     member private x.f3 = 42     static member private f4 = 42      member private this.somemethod() =         // "main" declaration has access values (a, f1 , func())         // members (f2, f3, f4)         printf "a: %d, f1: %d, f2: %d, f3: %d, f4: %d, func(): %d"             f1 this.f2 this.f3 sampletype.f4 (func())  // "partial" declaration type sampletype      member private this.anothermethod() =         // "partial" declaration has no access values (a, f1 , func())         // , following 2 lines won't compile         //printf "a: %d"         //printf "f1: %d" f1         //printf "func(): %d" (func())          // has access private members (f2, f3 , f4)         printf "f2: %d, f3: %d, f4: %d"             this.f2 this.f3 sampletype.f4 

i read f# specification didn't find ideas why f# compiler differentiate between value , member declarations.

in 8.6.1.3 section of f# spec said "the functions , values defined instance definitions lexically scoped (and implicitly private) object being defined.". partial declaration has access private members (static , instance). guess "lexical scope" specification authors mean "main" declaration behavior seems weird me.

the question is: behavior intentional , rationale behind it?

this great question! pointed out, specification says "local values lexically scoped object being defined", looking @ f# specification, not define lexical scoping means in case.

as sample shows, current behavior lexical scope of object definition primary type definition (excluding intrinsic extensions). i'm not surprised that, see other interpretation make sense too...

i think reason 2 kinds of extensions should behave same (as possible) , should able refactor code using 1 using other need. 2 kinds differ in how compiled under cover. property broken if 1 kind allowed access lexical scope while other did not (because, extension members technically cannot that).

that said, think (at least) clarified in specification. best way report send email fsbugs @ microsoft dot com.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -