caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] dynamic dispatching
@ 2003-06-10 10:09 Mirko Aigner
  2003-06-10 10:34 ` Ville-Pertti Keinonen
  2003-06-12 16:35 ` brogoff
  0 siblings, 2 replies; 4+ messages in thread
From: Mirko Aigner @ 2003-06-10 10:09 UTC (permalink / raw)
  To: caml-list

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

greetings Mirko Aigner

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] dynamic dispatching
  2003-06-10 10:09 [Caml-list] dynamic dispatching Mirko Aigner
@ 2003-06-10 10:34 ` Ville-Pertti Keinonen
  2003-06-10 23:33   ` Matt Gushee
  2003-06-12 16:35 ` brogoff
  1 sibling, 1 reply; 4+ messages in thread
From: Ville-Pertti Keinonen @ 2003-06-10 10:34 UTC (permalink / raw)
  To: Mirko Aigner; +Cc: caml-list

> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] dynamic dispatching
  2003-06-10 10:34 ` Ville-Pertti Keinonen
@ 2003-06-10 23:33   ` Matt Gushee
  0 siblings, 0 replies; 4+ messages in thread
From: Matt Gushee @ 2003-06-10 23:33 UTC (permalink / raw)
  To: caml-list

On Tue, Jun 10, 2003 at 01:34:49PM +0300, Ville-Pertti Keinonen wrote:
> 
> 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.

Yes. I'd say as a general principle, for both natural languages and
programming languages, that literal translations are mostly poor
translations. To do it well you have to be able to think in both
languages.

-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] dynamic dispatching
  2003-06-10 10:09 [Caml-list] dynamic dispatching Mirko Aigner
  2003-06-10 10:34 ` Ville-Pertti Keinonen
@ 2003-06-12 16:35 ` brogoff
  1 sibling, 0 replies; 4+ messages in thread
From: brogoff @ 2003-06-12 16:35 UTC (permalink / raw)
  To: Mirko Aigner; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-06-12 16:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-10 10:09 [Caml-list] dynamic dispatching Mirko Aigner
2003-06-10 10:34 ` Ville-Pertti Keinonen
2003-06-10 23:33   ` Matt Gushee
2003-06-12 16:35 ` brogoff

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