On Wed, 24 Oct 2012 20:40:27 +0200It's the simplest statement possible to demonstrate the typing error I
Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
> I don't really understand what you are trying to achieve with this
> #foo types.
ran into. Even simpler:
class type a = object end
and b =
object
method foo: 'a. (#a as 'a) -> unit
end
fails, but
class type a = object end
class type b =
object
method foo: 'a. (#a as 'a) -> unit
end
works fine. Why?
That's correct. It should read
> What would even be the type of "set" in your example? You
> cannot hold a mutable reference to a polymorphic type (#element list),
> so I'm not sure what you would have but (element list).
val mutable set = []
method register :'a. (#element as 'a) -> unit =set <- (s : #element :> element) :: set
fun s ->
That's my current workaround for this issue. But I would prefer a
> If you're
> going to coerce your elements into the common (element) supertype
> anyway, why insist on having flexible bounds? You could just use
> (registry) and (element), coerce when needed (foo :> element), and get
> rid of those pesky typing issues.
solution where the coercion happens in the registry.
>
> On Wed, Oct 24, 2012 at 8:03 PM, Christopher Zimmermann
> <madroach@gmerlin.de> wrote:
> > Hi,
> >
> > I have a problem with typing a system of mutually recursive classes.
> >
> > This piece of code fails to compile:
> >
> > class a =
> > object end
> > and b =
> > object
> > method foo: a -> int =
> > fun s -> 3
> > end;;
> >
> > Error: The universal type variable 'a cannot be generalized:
> > it escapes its scope.
> >
> >
> > But this compiles fine:
> >
> > class a =
> > object end
> > class b =
> > object
> > method foo: 'a. (#a as 'a) -> int =
> > fun s -> 3
> > end;;
> >
> >
> > What I actually want to do is this:
> >
> > class element id (registry :#registry) =
> > object
> > method registry = registry
> > end
> >
> > and registry =
> > object
> > val set = []
> > method register :'a. (#element as 'a) -> unit =
> > fun s ->
> > set <- s :: set
> > end
> >
> >
> > Any ideas how to do this without parametrizing the classes?
> >
> > Christopher
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs