[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brian Rogoff <bpr@b...> |
| Subject: | Re: classes mutually recursive |
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