I came across several places on the Internet where the code looked something like this:
[<CustomEquality;NoComparison>]
type Test =
| Foo
| Bar
override x.Equals y =
match y with
| :? Test as y' ->
match y' with
| Foo -> false
| Bar -> true
| _ -> failwith "error"
override x.GetHashCode() = hash x
But when I run the above in FSI, the invitation does not return when I either call hash fooin the instance Test, or when I call foo.GetHashCode()directly.
let foo = Test.Foo;;
hash foo;; // no returning to the console until Ctrl-break
foo.GetHashCode();; // no return
I could not readily confirm this, but it assumes that it hash xis calling GetHashCode()on the object, which means that the above code is dangerous. Or is it just an FSI game?
I thought that code like the one above only means “perform normal equality, but leave the default hash function”.
-, , , hash GetHashCode(), .
, FSI , , GetHashCode() , - . : , x.Equals GetHashCode(), Equals, GetHashCode().