Ocaml 无法键入多态[%bs.raw函数

Ocaml 无法键入多态[%bs.raw函数,ocaml,ffi,reason,bucklescript,value-restriction,Ocaml,Ffi,Reason,Bucklescript,Value Restriction,1) 有没有办法输入这些错误信息?2)有没有人能够解释这些错误信息 let identity1: 'a => 'a = [%bs.raw {| function(value) { return value } |}]; /* Line 2, 11: The type of this expression, '_a -> '_a, contains type variables that cannot be generalized */ let identity2:

1) 有没有办法输入这些错误信息?2)有没有人能够解释这些错误信息

let identity1: 'a => 'a = [%bs.raw {|
  function(value) {
    return value
  }
|}];

/*
Line 2, 11: The type of this expression, '_a -> '_a, contains type variables that cannot be generalized
*/

let identity2: 'a. 'a => 'a = [%bs.raw {|
  function(value) {
    return value
  }
|}];

/*
Line 8, 11: This definition has type 'a -> 'a which is less general than 'a0. 'a0 -> 'a0
*/

bs.原始的
是有效的(精确地说是扩展的),因此它受到价值限制:

P>简单地说,函数应用的结果的类型不能被概括,因为它可能已经捕获了一些隐藏的引用。例如,考虑函数:

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\u id
的下一个应用程序将是
3
。因此
not\u id
的类型不能是
∀'a、 “a->”a
。这就是为什么类型检查器会为您的函数推断类型
'\u weak1->'\u weak1
(使用4.06表示法)。此类型
\u weak1
不是多态类型,而是未知具体类型的占位符

在正常设置下,解决方案是使
not_id
为η-展开的值:

 let id x = fake_id () x 
 (* or *)
 let id: 'a. 'a -> 'a = fun x -> fake_id () x