繁体   English   中英

如何使用 map 作为 F# 中的类型?

[英]How can I use a map as a type in F#?

我有以下内容:

Map [
        "A", new Dictionary<int64, int64 * float>()
        "B", new Dictionary<int64, int64 * float>()
]

我想要一本这样的字典,比如:

let a = 
    new Dictionary<
        string,
        Map [
                "A", new Dictionary<int64, int64 * float>()
                "B", new Dictionary<int64, int64 * float>()
        ]>()

如何获取 map 的类型来创建字典?

目前尚不完全清楚您的问题是如何编写您想要的字典类型,或者您是否在询问如何构造这种类型的值。 Map 是一个泛型类型Map<'K, 'V> ,很像 Dictionary 的类型,所以要编写你要使用的类型:

Dictionary<string, Map<string, Dictionary<int64, int64 * float>>>

要创建一个值,您可以编写完整类型,或者您可以使用 F# 通常可以为您推断类型 arguments 的事实,因此您可以将它们替换为_ 例如:

let map = 
  Map [
      "A", new Dictionary<int64, int64 * float>()
      "B", new Dictionary<int64, int64 * float>()
  ]

let a = new Dictionary<_, _>(dict ["first", map])

我还要补充一点,词典地图词典是一种非常难以理解的类型。 将其中一些提取到简单的命名类中以传达有关类型含义的一些想法可能是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM