【发布时间】:2020-02-16 09:29:54
【问题描述】:
给定以下模块,编译器会引发错误
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);
}
这似乎是因为我只是将部分参数应用于reduceList。现在,我得到了一些关于价值限制的信息,在我看来,这就是这里发生的事情。
我已经尝试显式键入函数 min 和 max 并在其中显式键入模块作为一个整体,因为我认为根据 this section about value restriction 应该这样解决这个问题.但是,这似乎没有什么区别。
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);
};
另一方面...类型的前导 _ 在这里有什么特别的含义吗?
【问题讨论】:
标签: ocaml reason partial-application value-restriction