views:

643

answers:

1

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:

  1. Use an explicit class constructor (Cannot use type Foo(args) = syntax)
  2. Set the field to be static, mutable
  3. 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
This answer should be updated since you have to declare static variables private now.
gradbot