我想用Map初始化一个递归函数.要声明具有值的只读字典,我们可以这样做:
let dictionary1 = dict [ (1, "a"); (2, "b"); (3, "c") ]
Run Code Online (Sandbox Code Playgroud)
现在,我想创建一个空字典.怎么实现呢?
我正在寻找像:
let dictionary1 = {} // <- ok this is js
let (stuff, dictionary2) = parseRec("whatever", dictionary1)
Run Code Online (Sandbox Code Playgroud)
你可以做:
let dictionary1 = dict []
for elem in dictionary1 do
printfn "Key: %d Value: %s" elem.Key elem.Value
Run Code Online (Sandbox Code Playgroud)
这会给你一个空字典.
在printfn
使用揭示的类型推理引擎的正确类型的键和值的.
如果您想要明确,那么您可以通过多种方式指定类型:
let dictionary1 = dict Seq.empty<int * string>
let dictionary1 = dict ([] : (int * string) list)
let dictionary1 = dict [] : System.Collections.Generic.IDictionary<int, string>
let dictionary1 : System.Collections.Generic.IDictionary<int, string> = dict []
Run Code Online (Sandbox Code Playgroud)