8

F# には MAP があることは知っていますが、.NET ディクショナリを使用したいと考えています。この dict には、文字列としてのキーと F# 値 + dict としての値があります。

type ExprC = 
    | StrC of string
    | BoolC of bool
    | IntC of int32
    | DecC of decimal
    | ArrayC of int * array<ExprC>
    | RelC of RelationC
and RelationC = Dictionary<string, ExprC>        

さて、私が解決したい問題は、RelationC 型に構造的等価性を提供する方法です。実際のストレージをカプセル化する必要がある場合、どのようにディクショナリの代わりとなるコンテナを作成し、それを変更可能な操作に使用し、構造的な同等性を持たせますか?


現在の回答では、このコードは機能しません(実装は完全ではありませんが、これはコンパイルさえできません):

[<CustomEquality; CustomComparison>]
type MyDict() =
    inherit Dictionary<string, ExprC>()
    override this.Equals x =
        match x with
        | :? MyDict as y -> (this = y)
        | _ -> false

    override this.GetHashCode () =
        hash this

    interface System.IComparable with
      member x.CompareTo yobj =
          match yobj with
          | :? MyDict as y -> compare x y
          | _ -> invalidArg "MyDict" "cannot compare values of different types"

and [<StructuralEquality;StructuralComparison>] ExprC =
    | IntC of int
    | StrC of string
    | MapC of MyDict

これはエラーです:

エラー FS0377: この型は、属性 'NoEquality'、'ReferenceEquality'、'StructuralEquality'、'NoComparison'、および 'StructuralComparison' の無効な組み合わせを使用しています (FS0377)

4

2 に答える 2