Is there any way to access let bound fields from a static member? The following gives the indicated error:
type Foo(x) =
let x = x
static member test() =
let foo = Foo(System.DateTime.Now.Month)
printfn "%A" foo.x //the field, constructor or member 'x' is not defined
()
Whereas private explicit fields do allow access from static members:
type Bar =
val private x:int
new(x) = { x=x }
static member test() =
let Bar = Bar(System.DateTime.Now.Month)
printfn "%A" Bar.x
()
The documentation http://msdn.microsoft.com/en-us/library/dd469494.aspx states that “Explicit fields are not intended for routine use,” yet accessing private instance fields from static members is certainly a routine scenario. Moreover, I don’t believe you can set explicit fields within a primary constructor, which means if even one private instance field needs to be accessed from a static member, all of your fields must be moved over to explicit fields and you can no longer use a primary constructor — it’s all or nothing.
As real world example where you would actually want to access a private instance field from a static member, consider a big integer implementation: a BigInteger class would be immutable, so the internal representation of the big integer would kept as a private instance field (let’s call it data). Now, suppose you felt an Add(other) instance method was inappropriate for an immutable data structure and you only wanted to implement a static Add(lhs,rhs) method: in this case, you would need to be able to access lhs.data and rhs.data.
I don’t think you can do that… in fact, you can’t access let-bound values from other instances either:
In general, if you need to access such a value from outside of the instance it belongs to, you should probably either create a private property to get the value or use a private field instead.
UPDATE
This is covered by section 8.6.2 of the spec. In particular:
Perhaps someone from the F# team will weigh in with a definitive answer as to why the language behaves this way. However, I can think of a couple of potential reasons:
As mentioned earlier, here are a roughly equivalent class definition and method to create a record:
Since constructors in F# are conceptually similar to any other function which returns an instance of a type (e.g. they can be called without using
new), callingMyClass 5is conceptually similar to callingmakeMyRecord 5. In the latter case, we clearly don’t expect that there is any way to access the local let binding forjfrom another instance of the record. Therefore, it’s consistent that in the former case we also don’t have any access to the binding.