Fable icon indicating copy to clipboard operation
Fable copied to clipboard

[<FSharp.Core.DefaultValue>] not handled properly on static fields

Open chkn opened this issue 3 years ago • 1 comments

Description

When a static field has the [<FSharp.Core.DefaultValue>] attribute, Fable leaves its value as undefined, which can have different behavior than the .NET default value.

Repro code

type Foo() =
    [<FSharp.Core.DefaultValue>]
    static val mutable private count : int
    static member IncrCount() =
        Foo.count <- Foo.count + 1
        Foo.count

printfn "%d" (Foo.IncrCount())

Expected and actual results

dotnet fsi prints: 1 Fable REPL prints: 0

Related information

  • Fable version: 3.7.0-beta-011
  • Operating system: macOS 10.15.7

chkn avatar Jan 17 '22 02:01 chkn

I found a workaround: save the static field to a local first. This works as expected:

    static member IncrCount() =
        let count = Foo.count
        Foo.count <- count + 1
        Foo.count

chkn avatar Jan 17 '22 02:01 chkn