From: Brian Rogoff <bpr@best.com>
To: Sylvain Kerjean <skerjean@irisa.fr>
Cc: caml-list@inria.fr
Subject: Re: classes mutually recursive
Date: Wed, 31 Jan 2001 08:42:41 -0800 (PST) [thread overview]
Message-ID: <Pine.BSF.4.21.0101310832510.4347-100000@shell5.ba.best.com> (raw)
In-Reply-To: <3A7542CB.6731EBDC@irisa.fr>
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
prev parent reply other threads:[~2001-01-31 16:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-01-29 10:15 Sylvain Kerjean
2001-01-31 16:42 ` Brian Rogoff [this message]
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=Pine.BSF.4.21.0101310832510.4347-100000@shell5.ba.best.com \
--to=bpr@best.com \
--cc=caml-list@inria.fr \
--cc=skerjean@irisa.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