When I created a class containing a generic, mutable .NET Stack in F# like the example below, that stack ignores anything I push onto it.
open System.Collections.Generic
type Interp(code: int array) =
member val PC = 0 with get, set
member this.stack: Stack<int> = new Stack<int>()
member this.Code = code
let interp = Interp([|1;2;3|])
interp.stack.Push(1)
printfn "%A" interp.stack // prints "seq[]" WAT?!
Yet if I make the stack mutable via a property:
open System.Collections.Generic
type Interp(code: int array) =
member val PC = 0 with get, set
member val stack: Stack<int> = new Stack<int>() with get, set
member this.Code = code
let interp = Interp([|1;2;3|])
interp.stack.Push(1)
printfn "%A" interp.stack // prints "seq[1]"
Everything magically works like I'd expect.
What on earth is going on here? My understanding of immutability from previous languages (C# mostly) would say that even though the stack in the first example is an immutable member, that immutablity should only go as far the reference (aka I shouldn't be able to reassign Stack itself). I should still be able to push values to/from it. What am I missing, and if trying to mutate that stack is the wrong thing, why doesn't it throw an exception or a compile error?
If you try to compile the first version, and then use e.g. Reflector to decompile it to C#, you'll see that the stack member is defined like this:
As you can see, this is also valid C# code, but obviously not what you want.
The second version cross-compiles to something like this:
This seems more like what you'd want a property to do.