Is there a way to have a mutable static variable in F# class that is identical to a static variable in C# class ?
+3
A:
This isn't really reccomended, but if you must do it:
- Use an explicit class constructor (Cannot use type Foo(args) = syntax)
- Set the field to be static, mutable
- Add the [] attribute
type Foo =
new() = { }
[<DefaultValue>]
static val mutable m_field : int
Foo.m_field <- 1
printfn "Foo.m_field = %d" Foo.m_field
Chris Smith
2008-09-15 17:56:56
This answer should be updated since you have to declare static variables private now.
gradbot
2010-04-11 23:49:07