bs.raw
是有效的(准确地说是扩展的),因此它受到价值限制:
http://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html#sec51
。
简而言之,函数应用程序的结果类型无法概括,因为它可能捕获了一些隐藏的引用。例如,考虑函数:
let fake_id () = let store = ref None in fun y ->
match !store with
| None -> y
| Some x -> store := Some x; y
let not_id = fake_id ()
let x = not_id 3
然后下一次应用
not_id
将是
3
. 因此
not\U id
不能是
â'a. 'a -> 'a
. 这就是为什么类型检查器会为您的函数推断类型
'_weak1 -> '_weak1
(使用4.06符号)。此类型
_weak1
不是多态类型,而是未知具体类型的占位符。
在正常情况下,解决方案是
not\U id
具有·-扩展的值:
let id x = fake_id () x
(* or *)
let id: 'a. 'a -> 'a = fun x -> fake_id () x