I do not think there is a way to get a public static readonly field in F#.
If you use static let
, you end up with a private field (and, in fact, I think this is actually compiled as a private field with a private static property Xyz
):
type B() =
static let Xyz = 1
You can define actual fields with val
, but this does not support initialization (so you need to set it in a static constructor) and the compiler requires such fields to be private:
type A() =
static do A.Xyz <- 1
[<DefaultValue>]
static val mutable private Xyz : int
If you try to make the field public
, you will get:
error FS0881: Static 'val' fields in types must be mutable, private and marked with the '[]' attribute. They are initialized to the 'null' or 'zero' value for their type. Consider also using a 'static let mutable' binding in a class type.
I think the error message gives an answer to your question. That said, I cannot think of a good reason why you would need a static public field - a let
binding in a module should typically cover the use cases that you might have for a static public field (unless you need this because some library looks for static public fields via reflection...).