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 WAA10287; Sun, 8 Apr 2001 22:11:58 +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 WAA10293 for caml-list@pauillac.inria.fr; Sun, 8 Apr 2001 22:11:58 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA01769 for ; Fri, 6 Apr 2001 12:05:10 +0200 (MET DST) Received: from moutvdom00.kundenserver.de (moutvdom00.kundenserver.de [195.20.224.149]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f36A59528121 for ; Fri, 6 Apr 2001 12:05:09 +0200 (MET DST) Received: from [195.20.224.219] (helo=mrvdom03.kundenserver.de) by moutvdom00.kundenserver.de with esmtp (Exim 2.12 #2) id 14lT71-0000rn-00; Fri, 6 Apr 2001 12:05:03 +0200 Received: from drms-3e35783b.pool.mediaways.net ([62.53.120.59] helo=ice.gerd-stolpmann.de) by mrvdom03.kundenserver.de with esmtp (Exim 2.12 #2) id 14lT6k-00010y-00; Fri, 6 Apr 2001 12:04:46 +0200 Received: from localhost (localhost [[UNIX: localhost]]) by ice.gerd-stolpmann.de (8.9.3/8.9.3) id LAA03070; Fri, 6 Apr 2001 11:33:01 +0200 From: Gerd Stolpmann Reply-To: gerd@gerd-stolpmann.de Organization: privat To: "Nobuyuki Tomizawa" , Subject: Re: [Caml-list] Question: 'instanceof'-like like primitive in OCaml Date: Fri, 6 Apr 2001 11:14:38 +0200 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain References: In-Reply-To: MIME-Version: 1.0 Message-Id: <01040611330108.00489@ice> Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 04 Apr 2001, Nobuyuki Tomizawa wrote: >Dear all, > >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. I do not know the real reasons why the language designers refused to add these constructs. However, I know from OO literature that they are often considered as bad style, because they encourage programmers to mix object oriented and procedural elements. In Java you really need downcasts because Java lacks parametric polymorphism. For example, to use Java's stack class, you typically first cast your elements to Object (which is done implicitly), and when you want to get your elements back, you must downcast them to the class they previously had. In O'Caml, the container types like stack have a type parameter (such as int Stack.t or string Stack.t) so the compiler already knows the type of the elements; no downcasts are necessary as in Java. >My solution is to use variant type for the list and identify the class >using pattern matching: > > type tag = Derived2 of d2 | DontCare of b;; > > let l = [ Derived2(new d2); DontCare(new d1 :> b)] in ...;; > >But I feel this solution is awkward because we have to define variant >type for each classes I want to treat them as specific. > >Could you please tell me more 'smart' answer or another way in OCaml >style? The traditional OO style is what you want: class virtual base = object method common = ... method virtual specific : unit -> unit end class derived1 = object inherit base method specific() = () end class derived2 = object inherit base method specific() = your action end Now, simply call x#specific for _all_ elements x of the list. Gerd -- ---------------------------------------------------------------------------- Gerd Stolpmann Telefon: +49 6151 997705 (privat) Viktoriastr. 100 64293 Darmstadt EMail: gerd@gerd-stolpmann.de Germany ---------------------------------------------------------------------------- ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr