是否有替代 System.Collections.Generic.Dictionary
的替代方案?或 System.Collections.Hashtable
?
我对前者不满意,因为它使用 byref
返回值,即,我需要做烦人的
let x = ref ""
if hashtable.TryGetValue (key, x) then
// Found, value in !x
else
// Not found.
我对后者不满意,因为它不是通用的。
编辑。我更喜欢在语法上看起来像
Map.tryFind
的东西, IE。,match Hashtable.tryFind k hashtable with
| None -> ... // Not found
| Some v -> ... // Found v.
最佳答案
输出参数是 .NET 框架的一部分。然而,F# 确实通过自动将它们与返回值一起进行元组化来最大程度地减少痛苦。因此,使用 Dictionary<_,_>
你可以做:
match d.TryGetValue(key) with
| true, x -> ... //tuple of return value and out parameter
| _ -> ...
见 Passing by Reference on MSDN .
您可以轻松地将其包装到扩展名中:
type System.Collections.Generic.Dictionary<'K, 'V> with
member x.TryFind(key) =
match x.TryGetValue(key) with
| true, v -> Some v
| _ -> None
关于f# - F#中的哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921725/