本文介绍了歧视性工会结构/习惯平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下已区别的工会:
I have the following discriminated union:
type ActCard = Cellar of card list
| Chapel of (card option * card option* card option* card option)
| Smithy | Spy of (card -> bool * card -> bool)
它具有结构上的相等性,直到我将card -> bool
添加到Spy
为止. 此问题对于如何对记录进行自定义相等很有帮助.但是,我不确定在这种情况下如何最好地实施它.我希望不必在ActCard
中枚举每种情况:
It had structural equality until I added the card -> bool
to Spy
. This question is helpful for how to do custom equality for records. However, I'm not sure how best to implement it in this situation. I would prefer to not have to enumerate each case in ActCard
:
override x.Equals(yobj) =
match x, yobj with
| Spy _, Spy _ -> true
| Cellar cards, Cellar cards2 -> cards = cards2
(* ... etc *)
这里有什么更好的方法?
What is a better approach here?
推荐答案
没有更好的方法.如果您不打算使用默认的结构等式,则必须说明等式语义.
There isn't a better approach. If you're not going to use the default structural equality you'll have to spell out equality semantics.
您可以做这样的事情.
[<CustomEquality; CustomComparison>]
type SpyFunc =
| SpyFunc of (card -> bool * card -> bool)
override x.Equals(y) = (match y with :? SpyFunc -> true | _ -> false)
override x.GetHashCode() = 0
interface System.IComparable with
member x.CompareTo(y) = (match y with :? SpyFunc -> 0 | _ -> failwith "wrong type")
type ActCard =
| Cellar of card list
| Chapel of (card option * card option * card option * card option)
| Smithy
| Spy of SpyFunc
这篇关于歧视性工会结构/习惯平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!