Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] dynamic dispatching
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ville-Pertti Keinonen <will@e...>
Subject: Re: [Caml-list] dynamic dispatching
> 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