The common equality/comparison members design guideline is to not implement structural equality on mutable reference types, but take a look at F# record types with mutable fields:
type Value = { mutable value: int }
let mutableRecord = { value = 1 }
let xs = Map.ofList [ mutableRecord, "abc"
{ value = 2 }, "def" ]
let abc = Map.find { value=1 } xs
mutableRecord.value <- 3
let abc = Map.find { value=3 } xs // KeyNotFoundException!
The Map
is sorted internally, but mutable
record fields allows me to change ordering while record instance is already inside map and this is very bad.
I think F# should infer [<NoEquality>]
and [<NoComparison>]
modes for F# record types that declares mutable fields, isn't it?