From: "Nobuyuki Tomizawa" <tomizawa@ccm.cl.nec.co.jp>
To: <caml-list@inria.fr>
Subject: [Caml-list] Question: 'instanceof'-like like primitive in OCaml
Date: Wed, 4 Apr 2001 22:03:58 +0900 [thread overview]
Message-ID: <FNEDLLDONJLKENCGLOOIKENGCJAA.tomizawa@ccm.cl.nec.co.jp> (raw)
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
next reply other threads:[~2001-04-05 17:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-04-04 13:03 Nobuyuki Tomizawa [this message]
2001-04-05 18:15 ` Brian Rogoff
2001-04-05 19:09 ` Didier Le Botlan
2001-04-06 9:14 ` Gerd Stolpmann
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=FNEDLLDONJLKENCGLOOIKENGCJAA.tomizawa@ccm.cl.nec.co.jp \
--to=tomizawa@ccm.cl.nec.co.jp \
--cc=caml-list@inria.fr \
/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).