<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/11/96c5a045d70b126cf9ebbe6b346fc3dd"
  from="Damien &lt;Damien.Pous@e...&gt;"
  author="Damien"
  date="2003-11-03T10:46:38"
  subject="[Caml-list] Mutually recursive classes"
  prev="2003/11/927fa5865c499289949ce45512930e10"
  next="2003/11/5c281f3bb5dfafa71510a656eb10a809"
  next-in-thread="2003/11/5c281f3bb5dfafa71510a656eb10a809"
  prev-thread="2003/11/927fa5865c499289949ce45512930e10"
  next-thread="2003/11/ef8d11d2a3f4c6fd8183db8286734f46"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Mutually recursive classes">
<msg 
  url="2003/11/96c5a045d70b126cf9ebbe6b346fc3dd"
  from="Damien &lt;Damien.Pous@e...&gt;"
  author="Damien"
  date="2003-11-03T10:46:38"
  subject="[Caml-list] Mutually recursive classes">
<msg 
  url="2003/11/5c281f3bb5dfafa71510a656eb10a809"
  from="Didier Remy &lt;remy@m...&gt;"
  author="Didier Remy"
  date="2003-11-03T13:48:46"
  subject="Re: [Caml-list] Mutually recursive classes">
</msg>
<msg 
  url="2003/11/c347db9f99d8454f53064580a7b15faf"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-11-07T08:35:54"
  subject="Re: [Caml-list] Mutually recursive classes">
</msg>
</msg>
</thread>

<contents>
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 :
&gt;    constraint 'b = 'a #b0
&gt;                    ^^
&gt;This type &lt; b : 'b; coerce : 'a; .. &gt; as 'a should be an instance of
&gt;type 'c 
&gt;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

</contents>

</message>

