Search code examples
f#monogame

Why are MonoGame's Rectangle class fields immutable in F#?


    let rect = new Rectangle(x, y, sprite.Width, sprite.Height)
    rect.X <- 888

The second line fails in F#, but in C# the assignment works. I am not sure why.


Solution

  • Rectangle is a struct rather than a class. You can have that line work as you'd expect if you mark the value with mutable:

    let mutable rect = new Rectangle(10, 21, 10, 21)
    rect.X <- 888
    

    This is actually quite interesting, as F# makes the value-type semantics of structs more visible compared to what C# does.

    rect.X <- 888 does not really mutate the field, rather it updates rect with a new value of the struct with the X field modified. So you can imagine it does an equivalent of rect <- { rect with X = 888 }. At that point it's quite clear why the compiler wants to see mutable there.

    I wonder why F# designers didn't prohibit assignment syntax for structs and go with something similar to the record syntax. Probably they didn't want to make it too different compared to C#.