caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <gerd@gerd-stolpmann.de>
To: "Nobuyuki Tomizawa" <tomizawa@ccm.cl.nec.co.jp>, <caml-list@inria.fr>
Subject: Re: [Caml-list] Question: 'instanceof'-like like primitive in OCaml
Date: Fri, 6 Apr 2001 11:14:38 +0200	[thread overview]
Message-ID: <01040611330108.00489@ice> (raw)
In-Reply-To: <FNEDLLDONJLKENCGLOOIKENGCJAA.tomizawa@ccm.cl.nec.co.jp>

On Wed, 04 Apr 2001, Nobuyuki Tomizawa wrote:
>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.

I do not know the real reasons why the language designers refused to add these
constructs. However, I know from OO literature that they are often considered
as bad style, because they encourage programmers to mix object oriented
and procedural elements.

In Java you really need downcasts because Java lacks parametric polymorphism. 
For example, to use Java's stack class, you typically first cast your
elements to Object (which is done implicitly), and when you want to get your
elements back, you must downcast them to the class they previously had.

In O'Caml, the container types like stack have a type parameter (such as 
int Stack.t or string Stack.t) so the compiler already knows the type of the
elements; no downcasts are necessary as in Java.

>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?

The traditional OO style is what you want:

class virtual base = object
  method common = ...

  method virtual specific : unit -> unit
end

class derived1 = object
  inherit base

  method specific() = ()
end

class derived2 = object
  inherit base

  method specific() = your action
end

Now, simply call x#specific for _all_ elements x of the list.

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


      parent reply	other threads:[~2001-04-08 20:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-04 13:03 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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=01040611330108.00489@ice \
    --to=gerd@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=tomizawa@ccm.cl.nec.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).