Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Mutually recursive classes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien <Damien.Pous@e...>
Subject: [Caml-list] Mutually recursive classes
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