如何在Reason ML中声明地图类型? [英] How do I declare a map type in Reason ML?

查看:73
本文介绍了如何在Reason ML中声明地图类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Reason ML与JavaScript相比的一个优点是,它提供了一个Map类型,该类型使用结构相等而不是引用相等.

One advantage of Reason ML over JavaScript is that it provides a Map type that uses structural equality rather than reference equality.

但是,我找不到这种用法的示例.

However, I cannot find usage examples of this.

例如,我如何声明类型scores,它是字符串到整数的映射?

For example, how would I declare a type scores that is a map of strings to integers?

/* Something like this */
type scores = Map<string, int>; 

我将如何构造一个实例?

And how would I construct an instance?

/* Something like this */
let myMap = scores();

let myMap2 = myMap.set('x', 100);

推荐答案

标准库Map实际上在编程语言世界中是非常独特的,因为它是一个模块函子,您必须使用它来为您的Map构造一个映射模块.特定的密钥类型(因此Map.Make 下有 API参考文档) :

The standard library Map is actually quite unique in the programming language world in that it is a module functor which you must use to construct a map module for your specific key type (and the API reference documentation is therefore found under Map.Make):

module StringMap = Map.Make({
  type t = string;
  let compare = compare
});

type scores = StringMap.t(int);

let myMap = StringMap.empty;
let myMap2 = StringMap.add("x", 100, myMap);

还有其他数据结构可用于构造类似地图的功能,尤其是在您特别需要字符串键的情况下.有"BuckleScript食谱"中不同方法的比较.除Js.Dict以外的所有语言都可以在BuckleScript之外使用. BuckleScript还在Beta版标准库中随附了新的Map数据结构.我还没有尝试过.

There are other data structures you can use to construct map-like functionality, particularly if you need a string key specifically. There's a comparison of different methods in the BuckleScript Cookbook. All except Js.Dict are available outside BuckleScript. BuckleScript also ships with a new Map data structure in its beta standard library which I haven't tried yet.

这篇关于如何在Reason ML中声明地图类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆