given the following module the compiler raises an error
41 │ };
42 │
43 │ module TestB = {
44 │ let minFn = (a, b) => a < b ? a : b;
. │ ...
54 │ let max = reduceList(maxFn);
55 │ };
56 │
57 │ // module Number = {
The type of this module contains type variables that cannot be generalized:
{
let minFn: ('a, 'a) => 'a;
let maxFn: ('a, 'a) => 'a;
let reduceList: ('a, list('b)) => option('b);
let min: list('_a) => option('_a);
let max: list('_a) => option('_a);
}
This seems to happen because I'm only partially applying the arguments to reduceList
. Now, I've been provided some information about value restriction and it seems to me that this is what's happening here.
I've already tried explicitly typing the functions min
and max
where they are defined and to explicitly type the module as a whole because I thought that's how you're supposed to get around this according to this section about value restriction. However, this seems to make no difference.
module TestB = {
let minFn = (a, b) => a < b ? a : b;
let maxFn = (a, b) => a > b ? a : b;
let reduceList = (comp, numbers) =>
switch (numbers) {
| [] => None
| [head] => Some(head)
| [head, ...tail] => Some(List.fold_left(minFn, head, tail))
};
let min = reduceList(minFn);
let max = reduceList(maxFn);
};
On another note... does the leading _
for types mean anything special here?