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 QAA13169; Fri, 27 Sep 2002 16:59:05 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA13137 for caml-list@pauillac.inria.fr; Fri, 27 Sep 2002 16:59:04 +0200 (MET DST) 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 MAA04545 for ; Fri, 27 Sep 2002 12:16:37 +0200 (MET DST) Received: from swordfish.cs.caltech.edu (swordfish.cs.caltech.edu [131.215.44.124]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g8RAGZD17322; Fri, 27 Sep 2002 12:16:35 +0200 (MET DST) Received: from orchestra.cs.caltech.edu (orchestra.cs.caltech.edu [131.215.44.20]) by swordfish.cs.caltech.edu (Postfix) with ESMTP id 64A53DF23D; Fri, 27 Sep 2002 03:16:34 -0700 (PDT) Received: (from mvanier@localhost) by orchestra.cs.caltech.edu (8.11.6/8.9.3) id g8RAGYa27211; Fri, 27 Sep 2002 03:16:34 -0700 Date: Fri, 27 Sep 2002 03:16:34 -0700 Message-Id: <200209271016.g8RAGYa27211@orchestra.cs.caltech.edu> X-Authentication-Warning: orchestra.cs.caltech.edu: mvanier set sender to mvanier@cs.caltech.edu using -f From: Michael Vanier To: Francois.Pottier@inria.fr Cc: caml-list@inria.fr In-reply-to: <20020927092023.A32079@pauillac.inria.fr> (message from Francois Pottier on Fri, 27 Sep 2002 09:20:23 +0200) Subject: Re: [Caml-list] a design problem requiring downcasting? (long) References: <200209260901.g8Q910t03459@orchestra.cs.caltech.edu> <20020927092023.A32079@pauillac.inria.fr> Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk First off, thanks very much to all who have responded to my questions. I will have to spend the next few days trying to decipher your solutions. I anticipate it will be difficult, but interesting ;-) As to how I know what to do: the short answer is that I just do because I set up the simulation. All simulation objects will be subclasses of a base simulation object class (let's call it "sim_object"), and I want the array to be able to store any simulation object whatsoever. Thus, the type of the array has to be "array of sim_object" (assuming a pure OO approach, which is not mandatory). Fine so far. Now let's go further and assume that the sim_object class has a method "my_method". Then given an array element, standard OO single-dispatch polymorphism would allow you to access the version of this method for the array element's true class (which is a subclass of sim_object) when you invoke obj1#my_method. So if my_method didn't take any arguments you'd be done. But here it takes one argument (obj2), which is also a sim_object or one of its subclasses. The problem is, what it does with this argument depends strongly on what subclass it gets. Some subclasses it would just reject (throwing an exception). With others, it would do some processing. Therefore, ideally what I'd like is either: 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). 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. Now, the usual way these simulations are set up is that you create obj1, add it to the array (actually it's not an array but more like a tree structure, but never mind), then create obj2, add it to the array, and immediately invoke "obj1#my_method obj2". The "my_method" call actually is part of the simulation setup code. So you know that obj2 is of the appropriate subclass when you write the code. In java I believe the standard approach is to use instanceof and downcasting, but maybe this can be achieved in a better way using patterns or one of the functional approaches that have been proposed. Incidentally, I very much doubt whether I'll ever need more than double dispatch for this application (no triple+ dispatch, thank you very much). Thanks again for all the input, Mike > Date: Fri, 27 Sep 2002 09:20:23 +0200 > From: Francois Pottier > > Hi, > > > With alternative (1), to do the method call above you would need downcasting > > e.g. in pseudo-ocaml: > > > > let obj1 = array.(0) as class1 in > > let obj2 = array.(1) as class2 in > > obj1#my_method obj2 > > > > with the cast raising an exception if it fails. No problem. > > There is something you haven't explained here. How, given the array > indices 0 and 1, do you know that the appropriate classes are class1 > and class2? How do map from an index back to the expected class? If > we knew this, perhaps we would able to give better advice. > > -- > François Pottier > Francois.Pottier@inria.fr > http://pauillac.inria.fr/~fpottier/ > ------------------- 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