Following this question, I have created a file (and hence module) that defines a concrete Map
type:
/* Scores.re */
module StringMap = Map.Make({
type t = string;
let compare = compare
});
type scores = StringMap.t(int);
Now, I want to use the type in another file:
/* Demo.re */
let currentScores = Scores.scores.empty;
Js.log(currentScores);
However, this gives me the error:
The value scores can't be found in Scores
If I add a constant (e.g. let n = 123;
and Js.log(Scores.n);
) then it works.
What am I missing here?
scores
is a type, and types, even record types, do not have fields on the type itself. Furthermore, types and values live in different namespaces, so while the scores
type exists, the scores
value does not, hence the error "The value scores can't be found in Scores".
Modules, on the other hand, can have "fields", so that's why it lives there. And you could of course also alias empty
the same way you've aliased the Scores.t
type:
type scores = StringMap.t(int);
let empty = StringMap.empty;
Finally, you ask "Surely to make a Map instance the key-type must be known?". Indeed it does, and you've made it known. You specified the key type when you made the StringMap
module (Map.Make({ type t = string; ...0);
). You do not need to specify the value type (int
), however. That will be inferred.