私の他の回答にコメントについては、再利用可能なベースに、これを織り込むことができ私はそれが本当に良いアイデアだとは分かりません:
type EqCompBase<'EqKey,
'DerivedType when 'DerivedType :> EqCompBase<'EqKey,'DerivedType> >
(id : 'EqKey) =
member this.ID = id
override this.Equals(o) =
match o with
| :? EqCompBase<'EqKey, 'DerivedType> as sc -> this.ID = sc.ID
| _ -> false
override this.GetHashCode() =
id.GetHashCode()
interface System.IComparable with
member this.CompareTo(o) =
match o with
| :? EqCompBase<'EqKey, 'DerivedType> as sc -> compare this.ID sc.ID
| _ -> -1
type SomeClass(id : int, otherFieldThatDoesNotMatterForEquality : string) =
inherit EqCompBase<int, SomeClass>(id)
let someSet = Set.of_list [SomeClass(1,"yadda"); SomeClass(2,"blah")]
let test = someSet.Contains(SomeClass(2,"foo"))
printfn "%A" test // true
本当にありがとうございます、アクティブなレコードパターンを実装するF#パワーパックには何かありますか?私がちょうどそれを継承することができれば涼しいだろう。うーん、おそらく私はこのクラスにもっと追加し、そのように使用します。 – bhd739ge
他の回答を参照してください。 – Brian
辞書の代わりに辞書を使うことを考えましたか? – gradbot