Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: brogoff@s...
Subject: Re: [Caml-list] dynamic dispatching
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

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 Archives:
Bug reports: FAQ:
Beginner's list: