I have just started picking up F# as part of a personal project to implement Conways Game of Life in many different languages, and I’ve run into an issue when trying to set a value on a Dictionary within a class.
module World
open System.Collections.Generic
type World() =
member private this.cells = new Dictionary<string, int>()
member this.add_cell(): int =
printfn "%A" this.cells
this.cells.Add("0-0", 123)
printfn "%A" this.cells
this.cells["0-0"]
let world = new World()
world.add_cell() |> ignore
This gives me the following output:
seq []
seq []
Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key '0-0' was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at World.World.add_cell()
As you can see, there are some weird things happening.
- The this.cells, which should be a dictionary, is outputting as a seq ?
- Immediately after adding a value to the dictionary, the output of this.cells is still blank
- When trying to get the dictionary value using a key I just added, it does a runtime exception
Can anyone help me understand why this is the case? What am I missing here?
Thanks.