Consider the following code:
module Proxy = struct
type 'a t
end
class qObject proxy = object(self : 'self)
method proxy : 'self Proxy.t = proxy
end
class qWidget proxy = object(self : 'self)
inherit qObject proxy
method add : qWidget -> unit = fun w -> ()
method as_qWidget = (self :> qWidget)
end
class qButton proxy = object(self : 'self)
inherit qWidget proxy
method text = "button"
end
let qObject_proxy : qObject Proxy.t = Obj.magic 0
let qWidget_proxy : qWidget Proxy.t = Obj.magic 0
let qButton_proxy : qButton Proxy.t = Obj.magic 0
let qObject = new qObject qObject_proxy
let qWidget = new qWidget qWidget_proxy
let qButton = new qButton qButton_proxy
let () = qWidget
let () = qWidget
This code is well typed and compiled. But qButton must be manually added to qWidget, which I want to eliminate. I want qWidget # add to accept another qWidget or any derived class (e.g. qButton). I thought #qWidget would be the right type for this, but this does not work:
class qWidget proxy = object(self : 'self)
inherit qObject proxy
method add :
end
Error: Some type variables are unbound in this type: ...
The method add has type 'c -> unit where 'c is unbound
and
class qWidget proxy = object(self : 'self)
inherit qObject proxy
method add : 'a . (
end
Error: The universal type variable 'a cannot be generalized:
it escapes its scope.
Is there some way that I don't see?
source
share