relaxed value restriction

newってどうなってるんだろう。

まずは、コンストラクタ関数(?)から。

# let new_object x = object method get = x end;;
val new_object : 'a -> < get : 'a > = <fun>
# let a = new_object (fun x -> x);;
val a : < get : '_a -> '_a > = <obj>

この挙動はOK。

じゃあ、newは?

# class ['a] hoge (x:'a) = object method get = x end;;
class ['a] hoge : 'a -> object method get : 'a end
# let b = new hoge (fun x->x);;
val b : ('_a -> '_a) hoge = <obj>

これも単相になっちゃうのか…

# let c = new hoge (`A 3);;
val c : [> `A of int ] hoge = <obj>

ふむ。relaxed value restrictionかな。意外だった。安全に見えるんだけどな。

newがただの関数呼び出しとしてあつかわれているってことだろうか。


こういうのって、どっかに書いてないんでしょうかね。「OCamlのnewはいったい何なのか?」