Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 868095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:05:07+00:00 2026-05-15T10:05:07+00:00

Is there any way to access let bound fields from a static member? The

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:05:08+00:00Added an answer on May 15, 2026 at 10:05 am

    I don’t think you can do that… in fact, you can’t access let-bound values from other instances either:

    type Foo() =
      let x = 3
      member this.Test(f:Foo) =
        f.x // same error
    

    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:

    Instance “let” bindings are lexically scoped (and thus implicitly private) to the object being defined.

    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:

    1. let-bound values may not even be present as fields (e.g. again from the spec, a let binding will be represented by a local to the constructor “if the value is not a syntactic function, is not mutable and is not used in any function or member”)
    2. This seems consistent with the behavior of let bindings elsewhere in the language. See the examples of a roughly equivalent class and record definitions which I’ve included further down (because I can’t seem to properly format code blocks within an ordered list…)
    3. This provides a finer-grained level of encapsulation than is possible in many other languages – bindings which are local to the object being defined. Often, other instances will not need access to these bindings, in which case it’s nice not to expose them.
    4. If you want something which is accessible by other instances of your class (or from within static methods), there’s an easy way to do that – create a private field or property, which has the benefit of explicitly expressing your intention that the value be accessible from outside of the instance that you are in.

    As mentioned earlier, here are a roughly equivalent class definition and method to create a record:

    type MyClass(i:int) =
      let j = i * i
      member this.IsSameAs(other:MyClass) = 
        false // can't access other.j here
    
    type myRecord = { isSameAs : myRecord -> bool }
    let makeMyRecord(i:int) =
      let j = i * i
      { isSameAs = (fun r -> false) } //obviously, no way to access r.j here
    

    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), calling MyClass 5 is conceptually similar to calling makeMyRecord 5. In the latter case, we clearly don’t expect that there is any way to access the local let binding for j from another instance of the record. Therefore, it’s consistent that in the former case we also don’t have any access to the binding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to access the Windows Event Log from a java class.
Is there any way i can access the page object from within the global.asax
Is there any easy way to retrieve table creation DDL from Microsoft Access (2007)
Is there any way to access the file object used by a CSV writer/reader
Is there any way to access the <compilation /> tag in a web.config file?
Is there any way of coaxing Access 2007 into using a .UDL file? Specifically,
Is there any easy way to access the SVN repository revision number and store
Is there any way other than using reflection to access the members of a
Is there any way that I can programmatically create (and I guess access) hidden
Is there any way to do it? I only have client access and no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.