From: Alessandro Baretta <alex@baretta.com>
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] a design problem requiring downcasting? (long)
Date: Mon, 30 Sep 2002 00:59:29 +0200 [thread overview]
Message-ID: <3D9785D1.8080608@baretta.com> (raw)
In-Reply-To: <200209271016.g8RAGYa27211@orchestra.cs.caltech.edu>
Michael Vanier wrote:
> 1) multiple dispatch polymorphism (dispatching on the
types of both obj1
> and obj2). This is the most elegant approach, but
almost no languages
> support this (I think because it's very hard to
implement efficiently).
Oh, so what you really need is RTTI, not downcasting,
really. I cannot help you here. I think you need method
overloading for this one, and I doubt you'll get it soon in
O'Caml.
> 2) a way to do a typecase on obj2 to determine which
subclass of sim_object
> it is, so I can do the right thing.
Here's a quick implementation of RTTI in O'Caml which you
can use for your runtime switches
module Sim : sig
class type sim_obj_t = object ... end
val new_foo_sim : unit -> [> `Foo_sim of sim_obj_t ]
val new_bar_sim : unit -> [> `Bar_sim of sim_obj_t ]
end = struct
class type sim_obj_t = object end
class virtual sim_obj : sim_obj_t =
object (self)
end
class foo_sim : sim_obj_t =
object (self)
inherit sim_obj
end
let new_foo_sim () = `Foo_sim (new foo_sim)
class bar_sim : sim_obj_t =
object (self)
inherit sim_obj
end
let new_bar_sim () = `Bar_sim (new bar_sim)
end;;
let rtti_algorithm = function
| `Foo_sim x -> "Let's foo!"
| `Bar_sim x -> "Let's bar!"
| _ -> raise Not_found
***
You can place the RTTI dependent code within a method, thus
obtaining something like double-dispatch polymorphism. Of
course, you must explicitly code all cases you want your
code to handle within a single pattern-matching in each
class implementing your base class type; however, you get
the plus of not having to keep an explicit roster of all
your class type flags in one place.
You can also take advantage of the type checker by removing
the catch-all underscore and letting the compiler signal
what cases are missing from the pattern matching for it to
be complete.
I'm curious about this, so I would be glad if you let me
know what you end up doing.
Alex
-------------------
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
next prev parent reply other threads:[~2002-09-29 22:49 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-09-26 9:01 Michael Vanier
2002-09-26 14:32 ` Remi VANICAT
2002-09-26 15:19 ` nadji
2002-09-26 15:27 ` Remi VANICAT
2002-09-26 15:55 ` nadji
2002-09-26 15:53 ` Jeffrey Palmer
2002-09-26 16:35 ` Oleg
2002-09-26 17:47 ` brogoff
2002-09-26 19:14 ` Fred Smith
2002-09-27 17:01 ` Tim Freeman
2002-09-26 22:46 ` Alessandro Baretta
2002-09-27 7:20 ` Francois Pottier
2002-09-27 10:16 ` Michael Vanier
2002-09-29 22:59 ` Alessandro Baretta [this message]
2002-09-30 7:09 ` Michael Vanier
2002-09-30 9:54 ` Alessandro Baretta
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=3D9785D1.8080608@baretta.com \
--to=alex@baretta.com \
--cc=caml-list@inria.fr \
--cc=mvanier@cs.caltech.edu \
/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