From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA14011; Tue, 10 Jun 2003 12:34:52 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA14122 for ; Tue, 10 Jun 2003 12:34:51 +0200 (MET DST) Received: from mail.exomi.com (mail.exomi.com [217.169.64.72]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h5AAYoT13342 for ; Tue, 10 Jun 2003 12:34:51 +0200 (MET DST) Received: from exomi.com (oxy.exomi.com [10.0.20.56]) by mail.exomi.com (Postfix) with ESMTP id 325F45D03; Tue, 10 Jun 2003 13:34:50 +0300 (EEST) Date: Tue, 10 Jun 2003 13:34:49 +0300 Subject: Re: [Caml-list] dynamic dispatching Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v552) Cc: caml-list@inria.fr To: Mirko Aigner From: Ville-Pertti Keinonen In-Reply-To: <3EE5AE46.4050307@alcatel.de> Message-Id: <299DDADD-9B2F-11D7-8F2D-000393863F70@exomi.com> Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.552) X-Spam: no; 0.00; caml-list:01 dispatching:01 runtime:01 website's:99 multimethods:01 casts:01 implemented:01 bool:01 ocaml:01 typechecking:01 supported:01 string:03 classes:03 obj:03 btw:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 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