From: Leo White <lpw25@cam.ac.uk>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] implicit subtyping fails with recursive classes
Date: Sat, 20 Jul 2013 20:06:43 +0100 [thread overview]
Message-ID: <87bo5xcapo.fsf@study.localdomain> (raw)
In-Reply-To: <20130720131249.GA32103@frosties>
I am not sure, but I think the problem is that you are using #foo before
it is fully defined, so it is becoming unified with the universal 'a and
then when #foo is generalised the universal is escaping its scope.
One way to avoid the problem is to expand out the type of #foo:
# class type t = object method push : 'b. (< foo : t -> unit; ..> as 'b) -> unit end;;
class type t = object method push : < foo : t -> unit; .. > -> unit end
# class type foo = object method foo : t -> unit end;;
class type foo = object method foo : t -> unit end
if foo has a lot of methods, and you want to avoid typing them out
multiple times, you could try:
# class type ['a] foo' = object method foo : 'a -> unit end;;
class type ['a] foo' = object method foo : 'a -> unit end
# class type t = object method push : 'b. (t #foo' as 'b) -> unit end;;
class type t = object method push : t #foo' -> unit end
# class type foo = [t] foo';;
class type foo = [t] foo'
Regards,
Leo
Goswin von Brederlow <goswin-v-b@web.de> writes:
> Hi,
>
> I'm trying to add implicit subtyping to a pair of recurisve classes:
>
> Without subtyping:
> ------------------
> class type foo = object method foo : t -> unit end
> and t = object method push : foo -> unit end
>
> With that I can call t#push (x :> foo) for any x that implements the
> foo interface.
>
>
> What I want:
> ------------
> # class type foo = object method foo : t -> unit end
> and t = object method push : 'a . (#foo as 'a) -> unit end;;
> ^^^^^^^^^^^^^^^^^^^^^^^^^
> Error: The universal type variable 'a cannot be generalized:
> it escapes its scope.
>
>
>
> The problem seems to be the recursive definition with "and". Here is
> an example with and without "and":
>
> (* works *)
> class type foo = object method foo : string end;;
> class type bar = object inherit foo method bar : string end;;
> class type t = object method push : 'a . (#foo as 'a) -> unit end;;
>
> (* fails *)
> class type foo = object method foo : string end
> and bar = object inherit foo method bar : string end
> and t = object method push : 'a . (#foo as 'a) -> unit end;;
>
>
> So what am I doing wrong?
>
> MfG
> Goswin
next prev parent reply other threads:[~2013-07-20 19:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-20 13:12 Goswin von Brederlow
2013-07-20 19:06 ` Leo White [this message]
2013-07-23 11:54 ` Goswin von Brederlow
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=87bo5xcapo.fsf@study.localdomain \
--to=lpw25@cam.ac.uk \
--cc=caml-list@inria.fr \
--cc=goswin-v-b@web.de \
/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