value of CustomEquality and CustomComparison

旧巷老猫 提交于 2019-12-24 13:32:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!