私はF#にMAPがあることを知っていますが、.NET辞書を使用したいと考えています。今.NETでの変更可能な辞書<T, T> FでStructuralComparisonとEqualityを使用する方法
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の種類を提供する方法である:この辞書は、すなわちF#の値+ dictのように文字列としてキーと値を、持っています。実際のストレージをカプセル化する必要がある場合、ディクショナリの代わりとなるコンテナを作成する方法、変更可能な操作に使用する方法、構造的に同等であるかどうか現在の答えを
、このコードは動作しない(呪いの実装では、しかし、これはコンパイルすらない、完全なものではありません):
[<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
これはエラーです:
Error FS0377: This type uses an invalid mix of the attributes 'NoEquality', 'ReferenceEquality', 'StructuralEquality', 'NoComparison' and 'StructuralComparison' (FS0377)
好奇心で、 'Map'ではなく' Dictionary'でこれをしたいと思う特別な理由がありますか? – TheInnerLight
パフォーマンス。 .NETディクテーションは高速です(https://stackoverflow.com/questions/3396196/f-fsharpmap-vs-dictionary-performance)。私の使用事例では、私はそれをたくさん必要とします。プラス、好奇心。良い市民を.NETクラスに変換する方法を知っておくと便利だと思います) – mamcx
あなたはプロファイルを作成してパフォーマンスの問題を見つけましたか、それとも将来に問題があると思いますか?それが問題なのかどうか分からなければ、あなたの人生を困難にしないでください。それを明白な方法で行い、必要に応じて後で問題を解決してください。 – TheInnerLight