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 TAA00352; Thu, 5 Apr 2001 19:38:57 +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 TAA00477 for caml-list@pauillac.inria.fr; Thu, 5 Apr 2001 19:38:56 +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 PAA11701 for ; Wed, 4 Apr 2001 15:03:58 +0200 (MET DST) Received: from research.gate.nec.co.jp (research.gate.nec.co.jp [202.247.6.217]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f34D3tr18209 for ; Wed, 4 Apr 2001 15:03:56 +0200 (MET DST) Received: from triumph.swl.cl.nec.co.jp (postfix@triumph.swl.cl.nec.co.jp [10.56.50.3]) by research.gate.nec.co.jp (8.9.3+3.2W/000323) with ESMTP id WAA14241 for ; Wed, 4 Apr 2001 22:03:53 +0900 (JST) Received: from genesis.swl.cl.nec.co.jp (genesis.swl.cl.nec.co.jp [10.56.50.17]) by triumph.swl.cl.nec.co.jp (Postfix) with ESMTP id AB82D105906 for ; Wed, 4 Apr 2001 21:59:33 +0900 (JST) Received: from tomipc (swl-dhcp11 [10.56.50.211]) by genesis.swl.cl.nec.co.jp (8.10.2/SWL-00092619) with SMTP id f34D3oP27427 for ; Wed, 4 Apr 2001 22:03:53 +0900 (JST) From: "Nobuyuki Tomizawa" To: Subject: [Caml-list] Question: 'instanceof'-like like primitive in OCaml Date: Wed, 4 Apr 2001 22:03:58 +0900 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Importance: Normal Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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. 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? Thanks in advance. -- Nobuyuki ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr