以下类型扩展名
module Dict =
open System.Collections.Generic
type Dictionary<'K, 'V> with
member this.Difference(that:Dictionary<'K, 'T>) =
let dict = Dictionary()
for KeyValue(k, v) in this do
if not (that.ContainsKey(k)) then
dict.Add(k, v)
dict
Run Code Online (Sandbox Code Playgroud)
给出错误:
签名和实现不兼容,因为类型参数'TKey'的声明需要形式为'TKey:equality的约束
但是当我添加约束时,它会给出错误:
此类型扩展的声明类型参数与原始类型"Dictionary < , >" 上的声明类型参数不匹配
这尤其神秘,因为以下类型扩展没有约束并且有效.
type Dictionary<'K, 'V> with
member this.TryGet(key) =
match this.TryGetValue(key) with
| true, v -> Some v
| _ -> None
Run Code Online (Sandbox Code Playgroud)
现在我有一些奇怪的想法:只有在访问某些成员时才需要约束吗?