Confused about how to use Belt.Map.make


#1

The following code does not compile:

open Belt;
module IntCmp = Id.MakeComparable({
  type t = int;
  let cmp: (int, int) => int = Pervasives.compare;
})
let m = Map.make(~id=(module IntCmp));

The error I get is:

This expression's type contains type variables that can't be generalized:
  Belt.Map.t(IntCmp.t, '_a, IntCmp.identity)

  This happens when the type system senses there's a mutation/side-effect,
  in combination with a polymorphic value.
  Using or annotating that value usually solves it. More info:
  https://realworldocaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism

It seems to work fine if I try to make a Belt.Set using the same IntCmp module, so what is going on?


#2

Ah, I figured it out. If I add some more code that uses the map then type inferencing figures out the type of the Map’s value. For example:

m
|. Map.set((0,0), "origin")
|. Map.toArray
|. Js.log