Nic*_*ner 2 f# 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)
Run Code Online (Sandbox Code Playgroud)
它有结构平等,直到我添加card -> bool到Spy.此问题有助于如何为记录执行自定义相等.但是,我不确定在这种情况下如何最好地实现它.我宁愿不必列举每个案例ActCard:
override x.Equals(yobj) =
match x, yobj with
| Spy _, Spy _ -> true
| Cellar cards, Cellar cards2 -> cards = cards2
(* ... etc *)
Run Code Online (Sandbox Code Playgroud)
这里有什么更好的方法?
没有更好的方法.如果你不打算使用默认的结构相等,那么你必须拼出相等的语义.
你可以这样做.
[<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
Run Code Online (Sandbox Code Playgroud)