How to declare a map type in Reason ML?

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

However, I cannot find examples of using this.

For example, how can I declare a scores type, which is a string map for integers?

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

And how do I build an instance?

 /* Something like this */ let myMap = scores(); let myMap2 = myMap.set('x', 100); 
+5
source share
1 answer

The standard Map library is actually completely unique in the world of programming languages โ€‹โ€‹in that it is a module functor that you must use to build a map module for your specific key type (and the API reference documentation is in 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); 

There are other data structures that you can use to build such maps, especially if you need a string key. There is a comparison of the various methods in the Cookbook BuckleScript . Everything except Js.Dict is available outside of BuckleScript. BuckleScript also comes with a new map data structure in its beta standard library , which I have not tried yet.

+5
source

Source: https://habr.com/ru/post/1275404/


All Articles