From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA30212; Mon, 30 Sep 2002 00:49:45 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA30238 for ; Mon, 30 Sep 2002 00:49:44 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from athlon.baretta.com (r-mi214-6a44.tin.it [62.211.4.44]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g8TMnhD04137 for ; Mon, 30 Sep 2002 00:49:43 +0200 (MET DST) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id 73B6727249; Mon, 30 Sep 2002 00:59:29 +0200 (CEST) Message-ID: <3D9785D1.8080608@baretta.com> Date: Mon, 30 Sep 2002 00:59:29 +0200 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: Michael Vanier Cc: caml-list@inria.fr Subject: Re: [Caml-list] a design problem requiring downcasting? (long) References: <200209260901.g8Q910t03459@orchestra.cs.caltech.edu> <20020927092023.A32079@pauillac.inria.fr> <200209271016.g8RAGYa27211@orchestra.cs.caltech.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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