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 UAA01981; Thu, 5 Apr 2001 20:15:42 +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 UAA01732 for ; Thu, 5 Apr 2001 20:15:41 +0200 (MET DST) Received: from shell5.ba.best.com (shell5.ba.best.com [206.184.139.136]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f35IFe502754 for ; Thu, 5 Apr 2001 20:15:40 +0200 (MET DST) Received: from localhost (bpr@localhost) by shell5.ba.best.com (8.9.3/8.9.2/best.sh) with ESMTP id LAA05341; Thu, 5 Apr 2001 11:15:37 -0700 (PDT) Date: Thu, 5 Apr 2001 11:15:37 -0700 (PDT) From: Brian Rogoff To: Nobuyuki Tomizawa cc: caml-list@inria.fr Subject: Re: [Caml-list] Question: 'instanceof'-like like primitive in OCaml In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 4 Apr 2001, Nobuyuki Tomizawa wrote: > Dear all, > > I'm a novice OCaml programmer and have a question about heterogeneous > list and "downward cast". There are no downcasts in the OCaml object system. Someone posted a patch which would allow it but if you want to use standard OCaml you have to write code without it. It is bad OO style anyways, right? > 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? Actually, what you have isn't too bad, and you have to use variants to get the capability that you want. I do something like the following to get leaf and hier node object classes which can go on the same structure, maybe this is helpful to you. type ('a, 'b) node = Leaf_node of 'a | Hier_node of 'b type ('a,'b) instance = CellRef of (Atom.t * ('a, 'b) node * placement) | CellArrayRef of (Atom.t * ('a, 'b) node * placement * colrow * ipoint * ipoint) class virtual leaf_intf = object method virtual full_view : (leaf_intf, hier_intf) node ... end and virtual hier_intf = object inherit leaf_intf method virtual leaf_view : (leaf_intf, hier_intf) node method virtual insts : (leaf_intf, hier_intf) instance list end where instances will look like class some_leaf = object (self) inherit leaf_intf method full_view = Leaf_node (self :> leaf_intf) end class some_hier = object (self) inherit hier_intf method full_view = Hier_node (self :> hier_intf) method leaf_view = Leaf_node (self :> leaf_intf) ... end In all honesty though, when you need to do this kind of thing a lot you should consider writing the code using algebraic data types rather than classes. I think that people used to a mostly OO language should avoid the OO features until they are comfortable with the classic ML way. -- Brian ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr