caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien <Damien.Pous@ens-lyon.fr>
To: caml-list@inria.fr
Subject: [Caml-list] Mutually recursive classes
Date: Mon, 3 Nov 2003 11:48:16 +0100	[thread overview]
Message-ID: <20031103114816.50edea0c.Damien.Pous@ens-lyon.fr> (raw)

Hello,

I would like to create two mutually recursive classes,
parents (a) and children (b)

Furthermore, I'd like to define them incrementally
parents and children,
parents and children with hands
parents and children with hands and feet
...

let's have a try :

class type ['b] a0 = object ('a)
  method coerce: 'a
  method b: 'b
  constraint 'b = 'a #b0
  (* ... *)
end and ['a] b0 = object ('b)
  method coerce: 'b
  method a: 'a
  constraint 'a = 'b #a0
  (* ... *)
end

class type ['b] a1 = object ('a)
  inherit ['b] a0
  constraint 'b = 'a #b1
  (* ... *)
end and ['a] b1 = object ('b)
  inherit ['a] b0
  constraint 'a = 'b #a1
  (* ... *)
end

...


Unfortunately, this does not work :
>    constraint 'b = 'a #b0
>                    ^^
>This type < b : 'b; coerce : 'a; .. > as 'a should be an instance of
>type 'c 
>Self type cannot escape its class

which is odd from my mind, since the following code is well typed :

class type ['c] c = object
  method c: 'c
end
class type ['b] a = object ('a)
  method b: 'b
  constraint 'b = 'a #c
end


I can get something better, using two type parameters :

class type ['a, 'b] a0 = object
  method coerce: 'b
  method b: 'b
  constraint 'a = ('a, 'b) #a0
  constraint 'b = ('a, 'b) #b0
end and ['a, 'b] b0 = object
  method coerce: 'b
  method a: 'a
  constraint 'a = ('a, 'b) #a0
  constraint 'b = ('a, 'b) #b0
end

but then the "self type" is no longer correlated to 'a/'b  so that I
can't implement the method coerce, 
so, I need to "virtualize" it, and to implement it only at the end of
the class hierarchy, 
where I need to close it, saying 'a = 'self / 'b = 'self

I don't want this, because I'd like be able to use these classes at any
level (yes, a parent without feet does make sense...)


Does this case of "Self type cannot escape its class" restriction really
make sense ?

Do you know how to work-around this ?

thanks,
damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2003-11-03 10:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-03 10:48 Damien [this message]
2003-11-03 13:53 ` Didier Remy
2003-11-07  7:34 ` skaller
2003-11-03 17:41 pous damien
2003-11-04 17:52 ` Damien

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=20031103114816.50edea0c.Damien.Pous@ens-lyon.fr \
    --to=damien.pous@ens-lyon.fr \
    --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;
as well as URLs for NNTP newsgroup(s).