caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Question: 'instanceof'-like like primitive in OCaml
@ 2001-04-04 13:03 Nobuyuki Tomizawa
  2001-04-05 18:15 ` Brian Rogoff
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Nobuyuki Tomizawa @ 2001-04-04 13:03 UTC (permalink / raw)
  To: caml-list

Dear all,

I'm a novice OCaml programmer and have a question about heterogeneous
list and "downward cast".

Here is a pseudo Java code (I have):

     class base {
     	void common() {...};
     }

     class derived1 extends base {
     }

     class derived2 extends base {
	void specific() {..};
     } 

and what I want to do are:

     * make the list which typed as "base list".
     * call `derived2#specific' method if the element in the list is
     an instance of 'derived2'.

But, OCaml seems not to have Java's `instanceof'-like primitive and/or
downward-cast primitive.

My solution is to use variant type for the list and identify the class
using pattern matching:

    type tag = Derived2 of d2 | DontCare of b;;

    let l = [ Derived2(new d2); DontCare(new d1 :> b)] in ...;;

But I feel this solution is awkward because we have to define variant
type for each classes I want to treat them as specific.

Could you please tell me more 'smart' answer or another way in OCaml
style?

Thanks in advance.

-- Nobuyuki
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-04-08 20:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-04 13:03 [Caml-list] Question: 'instanceof'-like like primitive in OCaml Nobuyuki Tomizawa
2001-04-05 18:15 ` Brian Rogoff
2001-04-05 19:09 ` Didier Le Botlan
2001-04-06  9:14 ` Gerd Stolpmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).