method accept (v : 'a tree_visitor) : 'a = v#visit_leaf (self :> leaf) end
是行不通滴:
1 2 3 4 5
File "visitor.ml", line 3, characters 28-54: 3 | method virtual accept : 'a . 'a tree_visitor -> 'a ^^^^^^^^^^^^^^^^^^^^^^^^^^ Error: The universal type variable 'a cannot be generalized: it escapes its scope.
OCaml的类在定义时是单态的,比如:
1 2 3 4 5 6 7 8
class ['a] c (x:'a) = object method x = x end and d = object method strange = (new c 0)#x end
这里c是:
1
class ['a] c : 'a -> objectconstraint'a = intmethod x : 'aend