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 JAA08168; Thu, 7 Nov 2002 09:11:27 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA08245 for ; Thu, 7 Nov 2002 09:11:26 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gA78BO508260 for ; Thu, 7 Nov 2002 09:11:24 +0100 (MET) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id RAA15367; Thu, 7 Nov 2002 17:11:19 +0900 (JST) To: mvanier@cs.caltech.edu Cc: caml-list@inria.fr Subject: Re: [Caml-list] dynamic runtime cast in Ocaml In-Reply-To: <200211070201.gA721Qm17773@orchestra.cs.caltech.edu> References: <15817.713.977123.557926@hector.lesours> <20021106135428.A8640@pauillac.inria.fr> <200211070201.gA721Qm17773@orchestra.cs.caltech.edu> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20021107171119O.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 07 Nov 2002 17:11:19 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Michael Vanier > I understand some of the objections to downcasting, but what I'm really > curious about is this: if the ocaml team really *wanted* to allow downcasts > (raising an exception on failure just like dividing by zero does), would it > be technically feasible? By feasible I mean doable without nasty Obj.magic > hacks (no offense to coca-ml, which is very clever). Sure, it would be feasible. And a bit more efficient than coca-ml, since we could put the class identifier in a field rather than access it through a method. But it would mean maintaining more runtime data for a feature of disputed use. From this point of view coca-ml is a possible approach: it seems efficient enough, and you only use it when you really need downcasting. Note that coca-ml chooses to use Obj.magic, but there other ways to do downcasting which do not require any magic. The weak hash-table approach (see Weak_memo in Remy Vanicat's hweak library), or the exception approach are examples of ways to do that. And maybe someday we will see the light, and add a -with-rtti option to Objective Caml. But do not hold your breath, as there is a strong resistance to such an asymetric feature, which would work only on non-parametric classes. And this all the more as parametric classes are actually yet another way to solve the problem: class ['a] a (x : int) = object (self) val mutable x = x method x = x method me : 'a = `Ta (self :> _ a) end class ['a] b x = object (self) inherit ['a] a x method set_x y = x <- y method me : 'a = `Tb (self :> _ b) end # let some_b = new b 1;; val some_b : (_[> `Ta of '_a a | `Tb of '_a b] as 'a) b = # let hidden_b = (some_b :> _ a);; val hidden_b : (_[> `Ta of '_a a | `Tb of '_a b] as 'a) a = # match hidden_b#me with `Tb b -> b | _ -> assert false;; - : (_[> `Ta of '_b a | `Tb of 'a] as 'b) b as 'a = Ahem, types are ugly, but it works... Jacques Garrigue ------------------- 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