From: Christopher Zimmermann <madroach@gmerlin.de>
To: caml-list@inria.fr
Subject: [Caml-list] Re: typing mutually recursive classes
Date: Wed, 24 Oct 2012 21:32:13 +0200 [thread overview]
Message-ID: <20121024213213.2e447165b7c4ea9ad9d8e154@gmerlin.de> (raw)
In-Reply-To: <CAPFanBFpKi_0dCfrCp5_1d9rrhx5ykhJz=WJDFAaJkzDNW0xcA@mail.gmail.com>
On Wed, 24 Oct 2012 20:40:27 +0200
Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
> I don't really understand what you are trying to achieve with this
> #foo types.
It's the simplest statement possible to demonstrate the typing error I
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?
> 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).
That's correct. It should read
val mutable set = []
method register :'a. (#element as 'a) -> unit =
fun s ->
set <- (s : #element :> element) :: set
> 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.
That's my current workaround for this issue. But I would prefer a
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
next prev parent reply other threads:[~2012-10-24 19:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-24 18:03 [Caml-list] " Christopher Zimmermann
2012-10-24 18:40 ` Gabriel Scherer
2012-10-24 19:32 ` Christopher Zimmermann [this message]
2012-10-24 20:35 ` [Caml-list] " Didier Cassirame
2012-10-24 21:30 ` Didier Cassirame
[not found] ` <CAPFanBHdh8xKsZC6fs2oXEOitjKLhZzVUdVmYT+6d0jL46YM0w@mail.gmail.com>
2012-10-24 21:04 ` [Caml-list] " Gabriel Scherer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20121024213213.2e447165b7c4ea9ad9d8e154@gmerlin.de \
--to=madroach@gmerlin.de \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox