Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] a design problem requiring downcasting? (long)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-27 (14:59)
From: Michael Vanier <mvanier@c...>
Subject: Re: [Caml-list] a design problem requiring downcasting? (long)

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

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,


> 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
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: