重複の可能性:
タイプ拡張エラー
System.Collections.Generic.Dictionary に F# の拡張メソッドを追加したいと考えています。問題は、型制約を正しく取得できないように見えることです。私は次のようなものがうまくいくことを望んでいました:
type Dictionary<'k, 'd when 'k : equality> with
static member ofList (xs:list<'k * 'd>) : Dictionary<'k, 'd> =
let res = new Dictionary<'k, 'd> ()
for (k, d) in xs do
res.Add (k, d)
res
ただし、コンパイラは、私の宣言が Dictionary の宣言と異なると文句を言います。等式制約を省略しても、その特定のエラーは発生しません。しかし、それが見つからないことを警告します。できれば「警告レベルを下げる」というヒントに感謝します:-)
編集
私が望んでいた答えを提供してくれた KVB に感謝します。
type Dictionary<'k, 'd> with
static member ofList (xs:list<'k * 'd>) : Dictionary<'k, 'd> =
let res = new Dictionary<'k, 'd> (EqualityComparer<'k>.Default)
for (k, d) in xs do
res.Add (k, d)
res
編集:これは、RJ への返信をよりよく説明するための例です。コンパイラが推論できる場合、型をインスタンス化するときに型引数がオプションであることを示しています。警告やエラーなしでコンパイルされます。
type System.Collections.Generic.Dictionary<'k, 'd> with
static member test (dict:System.Collections.Generic.Dictionary<'k, 'd>) : bool =
dict.Values |> List.ofSeq |> List.isEmpty
let test (x:System.Collections.Generic.Dictionary<'k, 'd>) =
System.Collections.Generic.Dictionary.test x