如何在F#中将地图用作类型?

时间:2019-11-11 20:52:39

标签: 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>()
        ]>()

如何获取地图的类型以创建字典?

1 个答案:

答案 0 :(得分:4)

尚不清楚您的问题是如何编写所需字典的 type ,还是您正在询问如何构造该类型的值。 Map是一种通用类型Map<'K, 'V>,与Dictionary的类型非常相似,因此要编写您要使用的类型:

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

要创建值,可以编写完整类型,也可以使用F#经常为您推断类型参数的事实,因此可以将其替换为_。例如:

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

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

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