From: brogoff@speakeasy.net
To: Mirko Aigner <Mirko.Aigner@alcatel.de>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] dynamic dispatching
Date: Thu, 12 Jun 2003 09:35:41 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.44.0306120914570.7499-100000@grace.speakeasy.net> (raw)
In-Reply-To: <3EE5AE46.4050307@alcatel.de>
On Tue, 10 Jun 2003, Mirko Aigner wrote:
> Hi !!
>
> I'm new to OCAML and have the following question:
>
> I have to reimplement some code written in C++.
> In C++ there is used a desgin pattern named Visitor, which i used
> instead of a dynamic_cast.
> My question: is it generally possible to decide at runtime, if an object
> is of a specific type ?!
The simplest answer to this question is, "no". There is no Java style
instanceof in the language. If this is something you do frequently,
you would instead use sum types instead of classes.
If you insist on using classes, you can program the visitor pattern easily in
OCaml. Even though there is no method overloading, there are plenty of other
features which make up for it.
If you decide to use sum types, you may be troubled by inability to extend the
sum type without rewriting code. By using polymorphic variants, and hand coding
the open recursion using a fixpoint function, you can get around these
problems. The basic idea is here
http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/papers/fose2000.html
but be warned; if you're an FPL beginner this will make your eyes bleed and
your brains drip out your ears.
I suggest that you try to solve the problem in the core language (no OO or
polymorphic variants) + modules before anything else.
> What I have read in the reference manual and some website's dynamic
> typechecking is not supported, means this dynamic dispatching as well ?!
>
> I'm grateful for any answers ( positive or negative )
Good programming language choice, welcome aboard.
-- Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
prev parent reply other threads:[~2003-06-12 16:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-10 10:09 Mirko Aigner
2003-06-10 10:34 ` Ville-Pertti Keinonen
2003-06-10 23:33 ` Matt Gushee
2003-06-12 16:35 ` brogoff [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=Pine.LNX.4.44.0306120914570.7499-100000@grace.speakeasy.net \
--to=brogoff@speakeasy.net \
--cc=Mirko.Aigner@alcatel.de \
--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