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 WAA10138; Sun, 8 Apr 2001 22:07:55 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA10133 for caml-list@pauillac.inria.fr; Sun, 8 Apr 2001 22:07:54 +0200 (MET DST) 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 UAA02371 for ; Thu, 5 Apr 2001 20:57:27 +0200 (MET DST) Received: from mail.noos.fr (racine.noos.net [212.198.2.71]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f35IvQ503106 for ; Thu, 5 Apr 2001 20:57:26 +0200 (MET DST) Received: (qmail 7701346 invoked by uid 0); 5 Apr 2001 18:57:14 -0000 Received: from s165.dhcp212-198-87.noos.fr (HELO polytechnique.org) ([212.198.87.165]) (envelope-sender ) by racine.noos.net (qmail-ldap-1.03) with SMTP for ; 5 Apr 2001 18:57:14 -0000 Message-ID: <3ACCC302.3FE661E5@polytechnique.org> Date: Thu, 05 Apr 2001 21:09:54 +0200 From: Didier Le Botlan Organization: INRIA Rocquencourt X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.14-15mdk i586) X-Accept-Language: fr, en MIME-Version: 1.0 To: Nobuyuki Tomizawa CC: caml-list@inria.fr Subject: Re: [Caml-list] Question: 'instanceof'-like like primitive in OCaml References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hello, Because of strong typing, heterogeneous lists are not allowed (which is actually good). As far as I understand, what you need is - some kind of "instanceof" - some kind of dynamic cast (depending on the result of "instanceof"). You have many ways to get instanceof : For example you can declare a virtual method "instanceOfSpecific : bool" in base class, and each subclass must define it. Dynamic cast is not possible here because a base object cannot be seen as a "derived2" object in general. You can declare a method "specific" which raises an exception (default behaviour) and which is overriden in class "derived2" to do what expected. Thus, if you incorrectly call "specific" method on a "derived1" object, an exception is raised. This corresponds somehow to an incorrect dynamic cast in Java. Finally, here is a short example : class virtual base = object method common x = x+1 method virtual isSpecific : bool (* 'specific' returns a string. failwith is polymorph, we must help type checker. *) method specific = ((failwith "specific is not defined !!!") : string) end class derived1 = object inherit base method isSpecific = false end class derived2 message = object inherit base method isSpecific = true method specific = "Hello " ^ message end let example = print_endline "Starting..."; let obj1 = new derived1 and obj2 = new derived2 "World" and obj3 = new derived2 "Folks" in let list = [ obj1 ; obj2 ; obj3; obj2 ; obj1 ] in let apply_specific obj = if obj#isSpecific then print_endline obj#specific else print_endline "No Specific" in List.iter apply_specific list compile : ocamlc -o exam exam.ml and try... Nobuyuki Tomizawa wrote: > I'm a novice OCaml programmer and have a question about heterogeneous > list and "downward cast". > > Here is a pseudo Java code (I have): > > class base { > void common() {...}; > } > > class derived1 extends base { > } > > class derived2 extends base { > void specific() {..}; > } > > and what I want to do are: > > * make the list which typed as "base list". > * call `derived2#specific' method if the element in the list is > an instance of 'derived2'. > > But, OCaml seems not to have Java's `instanceof'-like primitive and/or > downward-cast primitive. ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr