我试图找出一种方法来向模块的用户隐藏某些辅助函数和相关的东西,并认为使用 IIFE 会起作用,但它失败了,因为类型变量不能被泛化?
我想我已经使用以下代码将其归结为最基本的场景:
module TestA = {
let y = 0;
let x = (type a, numbers: list(a)): option(a) => None;
};
module TestB = {
let x =
(
() => {
let y = 0;
(type a, numbers: list(a)): option(a) => None;
}
)();
};
在 TestB 中,编译器抱怨
41 │ };
42 │
43 │ module TestB = {
44 │ let x =
. │ ...
50 │ )();
51 │ };
52 │
53 │ module Number = {
The type of this module contains type variables that cannot be generalized:
{ let x: list('_a) => option('_a); }
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
这是为什么?我该如何解决y
对模块用户隐藏的问题?
Ps:当重新格式化返回类型注释时,TestB
会放在后面None
这样:(type a, numbers: list(a)) => (None: option(a))
。为什么在这里而不是在模块中TestA
?据我了解,这只是“标记”返回的值,所以我在这里看不出区别吗?