简体   繁体   English

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

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

I have the following:我有以下内容:

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

and I would like a dictionary of those, something like:我想要一本这样的字典,比如:

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

How to I get the type for the map to create the dictionary?如何获取 map 的类型来创建字典?

It is not entirely clear whether your question is how to write the type of the dictionary you want or whether you are asking how to construct a value of this type.目前尚不完全清楚您的问题是如何编写您想要的字典类型,或者您是否在询问如何构造这种类型的值。 Map is a generic type Map<'K, 'V> , much like the type of the Dictionary, so to write the type you'd use: Map 是一个泛型类型Map<'K, 'V> ,很像 Dictionary 的类型,所以要编写你要使用的类型:

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

To create a value, you can write the full type, or you can use the fact that F# can often infer the type arguments for you and so you can replace them with _ .要创建一个值,您可以编写完整类型,或者您可以使用 F# 通常可以为您推断类型 arguments 的事实,因此您可以将它们替换为_ For example:例如:

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

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

I would also add that dictionary of maps of dictionaries is a pretty incomprehensible type.我还要补充一点,词典地图词典是一种非常难以理解的类型。 It would probably be a good idea to extract some of those into simple named classes that convey some idea about the meaning of the type.将其中一些提取到简单的命名类中以传达有关类型含义的一些想法可能是一个好主意。

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

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