问题
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 ?
回答1:
[<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.
来源:https://stackoverflow.com/questions/16594719/value-of-customequality-and-customcomparison