> type c = class
member self.f = fun x -> x
end;;
The error message I got:
Value restriction. Type inference has inferred the polymorphic signature member c.f : ('_a -> '_a) but this member is a constructor or property getter/setter, which cannot be more generic than the enclosing type. Add type constraints to indicate the exact types involved.In fact, I simply need to add a type parameter.
> type 'a c = class
member self.f: 'a -> 'a = fun x -> x
end;;
> type c1 = class
new (x:int) = {}
end
> type c2 = class
inherit c1
new () = {}
end
cause a type error:
The method or object constructor c1 takes 1 arguments but is here supplied with 0.Since the constructor of c1 is implicitly called in the constructor body of c2.
The required signature is new : x:int -> c1.
> type c2 = class
inherit c1
new () = { inherit c1 (0) }
end
The error is also avoidable by adding c1
with a unit-valued constructor as:
> type c1 = class
new () = {}
new (x:int) = {}
end
Personally, I do not have a big liking for "implicit" things in general.
A result of this difference can be observed in the following experiment.
On the one hand, the following definitions are not accepted:
> type a1 = class new (x:int) = {} end;;
> type a2 = class inherit a1 end;;
On the other hand, these are accepted:
> type b1 = class end > type b2 = class inherit b1 endPersonally I somehow feel uncomfortable with this design choice, though it looks consistent.
By the way, observing that the following definitions are accepted:
> type c1 = class static member f x = x + 1 end;; > type c2 = class inherit c1 end;;I guess that a class definition with a static member is not a value definition, letting the member belong to the type that the definition generates.
> let f x = match x with X -> X val f : 'a -> 'a