Item 的静态约束
Static constraint on Item
我正在尝试编写 "TryGetValue" 的选项值版本,它适用于任何实现 IDictionary 或 IReadOnlyDictionary 的对象。我有这个:
let inline contains (key:^key) (dictionary:^dic )=
(^dic: (member ContainsKey: ^key -> bool) (dictionary, key) )
let inline tryGetValue (dictionary:^dic ) (key:^key)=
if contains key dictionary then
let value = ( ^dic : (member get_Item: ^key -> ^value ) (dictionary, key) ) (dictionary) ) key
value |> Some
else None
"value" 的定义产生警告,名称为 get_Item 的成员约束具有特殊状态,可能会导致运行时错误。我应该在这里做什么?
用 TryGetValue
代替 ContainsKey
和 Item
怎么样?
let inline tryGetValue dic key =
let mutable value = Unchecked.defaultof< ^value>
let contains = (^dic : (member TryGetValue : ^key * byref< ^value> -> bool) (dic, key, &value))
if contains then Some value else None
我正在尝试编写 "TryGetValue" 的选项值版本,它适用于任何实现 IDictionary 或 IReadOnlyDictionary 的对象。我有这个:
let inline contains (key:^key) (dictionary:^dic )=
(^dic: (member ContainsKey: ^key -> bool) (dictionary, key) )
let inline tryGetValue (dictionary:^dic ) (key:^key)=
if contains key dictionary then
let value = ( ^dic : (member get_Item: ^key -> ^value ) (dictionary, key) ) (dictionary) ) key
value |> Some
else None
"value" 的定义产生警告,名称为 get_Item 的成员约束具有特殊状态,可能会导致运行时错误。我应该在这里做什么?
用 TryGetValue
代替 ContainsKey
和 Item
怎么样?
let inline tryGetValue dic key =
let mutable value = Unchecked.defaultof< ^value>
let contains = (^dic : (member TryGetValue : ^key * byref< ^value> -> bool) (dic, key, &value))
if contains then Some value else None