caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ville-Pertti Keinonen <will@exomi.com>
To: Mirko Aigner <Mirko.Aigner@alcatel.de>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] dynamic dispatching
Date: Tue, 10 Jun 2003 13:34:49 +0300	[thread overview]
Message-ID: <299DDADD-9B2F-11D7-8F2D-000393863F70@exomi.com> (raw)
In-Reply-To: <3EE5AE46.4050307@alcatel.de>

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

If you're using the visitor design pattern, that probably means that 
you don't need to check the type at runtime.

> What I have read in the reference manual and some website's dynamic 
> typechecking is not supported, means this dynamic dispatching as well 
> ?!

If by dynamic dispatching you're referring to methods declared virtual 
in C++ -- all methods are like this in OCaml.

If you're referring to multimethods, neither C++ nor OCaml has that.

Anyway, if you aren't using dynamic_cast (or any other type-narrowing 
casts) in the C++ version, then the OCaml version can be implemented 
pretty much the same.

If you really need it, something similar to C++ dynamic_cast can be 
simulated in OCaml.  One way to do this is by implementing a method in 
your classes that you can use to check the type (e.g. a method is_type 
: string -> bool) and then using Obj.magic to "cast" the object to the 
confirmed type if successful (although Obj.magic isn't something you 
should generally use; it's basically equivalent to C++ 
reinterpret_cast).

BTW: Whenever you end up using the visitor design pattern, that may 
mean that you'd be better off separating the data from the functions 
manipulating it, not using classes at all.  OOP is highly overrated.

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


  reply	other threads:[~2003-06-10 10:34 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 [this message]
2003-06-10 23:33   ` Matt Gushee
2003-06-12 16:35 ` brogoff

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=299DDADD-9B2F-11D7-8F2D-000393863F70@exomi.com \
    --to=will@exomi.com \
    --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;
as well as URLs for NNTP newsgroup(s).