caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* classes mutually recursive
@ 2001-01-29 10:15 Sylvain Kerjean
  2001-01-31 16:42 ` Brian Rogoff
  0 siblings, 1 reply; 2+ messages in thread
From: Sylvain Kerjean @ 2001-01-29 10:15 UTC (permalink / raw)
  To: caml-list

I have to program a class A whose method m produces an object of class
B,with the property that B inherits from A :

class A =
...
method m = new B
...
end;;

class B =
...
inherit A
...
end;;

Of course it leads to a type clash, but as the manual exhibits an
example of the observer/notifier
problem, if someone could explain me a little more about a trick to get
the behaviour i need, it would be very kind !!

PS : sorry for my poor english, and no i can not change my architecture
of classes cause i interface an existing API (for those who are
interested, it is the architecture of the BHandler/BLooper in BeOS)


-- 
Sylvain Kerjean
IRISA-INRIA, Campus de Beaulieu, 35042 Rennes cedex, France
Tél: +33 (0) 2 99 84 75 99, Fax: +33 (0) 2 99 84 71 71



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: classes mutually recursive
  2001-01-29 10:15 classes mutually recursive Sylvain Kerjean
@ 2001-01-31 16:42 ` Brian Rogoff
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Rogoff @ 2001-01-31 16:42 UTC (permalink / raw)
  To: Sylvain Kerjean; +Cc: caml-list

On Mon, 29 Jan 2001, Sylvain Kerjean wrote:
> I have to program a class A whose method m produces an object of class
> B,with the property that B inherits from A :
> 
> class A =
> ...
> method m = new B
> ...
> end;;
> 
> class B =
> ...
> inherit A
> ...
> end;;

If both classes belong to the same module, simply connecting their
definitions with an "and" will allow a mutually recursive class (and
type!) definition. Here's a made-up example

# class foo = 
  object 
    method make_bar = new bar
    method print = print_endline "foo" 
  end
and bar = 
  object 
    inherit foo    
    method print = print_endline "bar" 
  end;;
                  class foo : object method make_bar : bar method print
: unit end
class bar : object method make_bar : bar method print : unit end
# let f = new foo;;                      
val f : foo = <obj>
# let b = f#make_bar;;                   
val b : bar = <obj>
# f#print;;
foo
- : unit = ()
# b#print;;
bar
- : unit = ()

If you'd like the classes to reside in different modules, then the trick
is to create interfaces or classes which break the mutual recursion, and 
inherit from those instead, since recursive modules are not yet supported
by OCaml. Another made-up example. The following won't work

module type DoctorType = 
  sig 
    class c : 
      object 
        method treat_patient : PatientType.c 
        method bill_patient : PatientType.c 
      end 
  end ;;

module type PatientType = 
  sig 
    class c : 
      object 
        method visit_doctor : DoctorType.c 
        method pay_doctor : DoctorType.c 
      end 
  end ;;

so we add a parent module which expresses an interface dependency and
write the signatures in terms of that. 

module Forwards = 
  sig 
    class virtual patient_intf = 
      object 
        method virtual visit_doctor : doctor_intf  
        method virtual pay_doctor :   doctor_intf
      end 
    and virtual doctor_intf = 
      object 
        method virtual treat_patient : patient_intf
        method virtual bill_patient  : patient_intf
      end 
  end ;;

module type DoctorType = 
  sig 
    class c : 
      object 
        method treat_patient : Forwards.patient_intf
        method bill_patient : Forwards.patient_intf
      end 
  end ;;

module type PatientType = 
  sig 
    class c : 
      object 
        method visit_doctor : Forwards.doctor_intf
        method pay_doctor : Forwards.doctor_intf
      end 
  end ;;

> Of course it leads to a type clash, but as the manual exhibits an 
> example of the observer/notifier
> problem, if someone could explain me a little more about a trick to get
> the behaviour i need, it would be very kind !!

I recommend that you read Didier Remy's excellent paper on this topic,
which is on his web page. 

> PS : sorry for my poor english, and no i can not change my architecture
> of classes cause i interface an existing API (for those who are
> interested, it is the architecture of the BHandler/BLooper in BeOS)

Hah, if my French were as good as your English, I wouldn't be apologizing. 
Fortunately this mailing list is helping ;-)

-- Brian




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-01-31 16:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-01-29 10:15 classes mutually recursive Sylvain Kerjean
2001-01-31 16:42 ` Brian Rogoff

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).