2

私は次の差別された組合を持っています:

type ActCard = Cellar of card list 
                | Chapel of (card option * card option* card option* card option) 
                | Smithy | Spy of (card -> bool * card -> bool)

に を追加するまで、構造的に同等でしcard -> boolSpyこの質問は、レコードのカスタム等価を行う方法に役立ちます。ただし、この状況でどのように実装するのが最善かはわかりません。で各ケースを列挙する必要はありませんActCard

override x.Equals(yobj) =
    match x, yobj with
    |  Spy _, Spy _ -> true
    |  Cellar cards, Cellar cards2 -> cards = cards2
    (* ... etc *)

ここでより良いアプローチは何ですか?

4

1 に答える 1

9

より良いアプローチはありません。デフォルトの構造的平等を使用しない場合は、平等のセマンティクスを詳しく説明する必要があります。

編集

このようなことができます。

[<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
于 2012-06-12T20:23:17.987 に答える