1

I understand the value of asserting

[<StructuralEquality;StructuralComparison>]

This statically forces equality and comparison constraints to be derived structurally, and have a nice side effect to warn if it can not

Similarly [<ReferenceEquality>] forces the equality constraint to be satisfied using reference.

Last NoComparison, NoEquality statically unsatisfy those constraints, with the benefit of catching errors as well.

However I am unsure what the added value of CustomEquality, CustomComparison is.

What is the added value of statically declaring that you'll do something custom ?

nicolas
  • 9,549
  • 3
  • 39
  • 83

1 Answers1

2

[<CustomEquality>] and [<CustomComparison>] are used when you have a record or union type and you need to define some non-reference, non-structural equality for it. The F# compiler normally generates the the equality and comparison functions for these types automatically, so the attributes tell the compiler not to generate those functions but to use your custom method instead.

An example of this could be if you have a record type which represents a database row, and (for whatever reason) you want to define two instances of the type as being equal if they have the same primary key value (e.g., CustomerId) -- even if the rest of the data they contain is different.

Jack P.
  • 11,487
  • 1
  • 29
  • 34
  • So their added value is to escape *generating* comparer (and binding to it at compile time) etc.. and have nothing to do with *satisfying* the constraints (which are erased and have nothing to do with runtime). – nicolas May 17 '13 at 08:56