From: Brian Rogoff <bpr@best.com>
To: Nobuyuki Tomizawa <tomizawa@ccm.cl.nec.co.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Question: 'instanceof'-like like primitive in OCaml
Date: Thu, 5 Apr 2001 11:15:37 -0700 (PDT) [thread overview]
Message-ID: <Pine.BSF.4.21.0104051058580.18792-100000@shell5.ba.best.com> (raw)
In-Reply-To: <FNEDLLDONJLKENCGLOOIKENGCJAA.tomizawa@ccm.cl.nec.co.jp>
On Wed, 4 Apr 2001, Nobuyuki Tomizawa wrote:
> Dear all,
>
> I'm a novice OCaml programmer and have a question about heterogeneous
> list and "downward cast".
There are no downcasts in the OCaml object system. Someone posted a patch
which would allow it but if you want to use standard OCaml you have to
write code without it. It is bad OO style anyways, right?
> 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?
Actually, what you have isn't too bad, and you have to use variants to
get the capability that you want. I do something like the following to
get leaf and hier node object classes which can go on the same structure,
maybe this is helpful to you.
type ('a, 'b) node = Leaf_node of 'a | Hier_node of 'b
type ('a,'b) instance =
CellRef of (Atom.t * ('a, 'b) node * placement)
| CellArrayRef of (Atom.t * ('a, 'b) node * placement * colrow * ipoint * ipoint)
class virtual leaf_intf =
object
method virtual full_view : (leaf_intf, hier_intf) node
...
end
and virtual hier_intf =
object
inherit leaf_intf
method virtual leaf_view : (leaf_intf, hier_intf) node
method virtual insts : (leaf_intf, hier_intf) instance list
end
where instances will look like
class some_leaf =
object (self)
inherit leaf_intf
method full_view = Leaf_node (self :> leaf_intf)
end
class some_hier =
object (self)
inherit hier_intf
method full_view = Hier_node (self :> hier_intf)
method leaf_view = Leaf_node (self :> leaf_intf)
...
end
In all honesty though, when you need to do this kind of thing a lot you
should consider writing the code using algebraic data types rather than
classes. I think that people used to a mostly OO language should avoid the
OO features until they are comfortable with the classic ML way.
-- Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-04-05 18:15 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 [this message]
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=Pine.BSF.4.21.0104051058580.18792-100000@shell5.ba.best.com \
--to=bpr@best.com \
--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).