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 RAA15874; Fri, 19 Sep 2003 17:49:38 +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 RAA08056 for ; Fri, 19 Sep 2003 17:49:37 +0200 (MET DST) Received: from mail.speakeasy.net (mail11.speakeasy.net [216.254.0.211]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h8JFna507078 for ; Fri, 19 Sep 2003 17:49:36 +0200 (MET DST) Received: (qmail 14740 invoked from network); 19 Sep 2003 15:49:34 -0000 Received: from unknown (HELO apprentice.genxnet.com) ([64.81.145.152]) (envelope-sender ) by mail11.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 19 Sep 2003 15:49:34 -0000 Date: Fri, 19 Sep 2003 10:58:52 -0500 From: art yerkes To: caml-list@inria.fr Subject: Re: [Caml-list] Subclasses and pattern matching Message-Id: <20030919105852.24b1ea03.ayerkes@speakeasy.net> In-Reply-To: <20030919152252.GA30989@henchmonkey.org> References: <20030919142156.GA30187@henchmonkey.org> <20030919145220.GD4205@redhat.com> <20030919152252.GA30989@henchmonkey.org> X-Mailer: Sylpheed version 0.8.11 (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; yerkes:01 ayerkes:01 caml-list:01 model:01 extern:01 extern:01 ints:01 inherit:01 inherit:01 speakeasy:01 int:01 int:01 sep:01 match:02 match:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, 19 Sep 2003 11:22:52 -0400 katre wrote: > Richard Jones wrote: > > > > Possibly I'm being dumb here, but wouldn't it be better to use > > a union type, eg: > > > > type num = Int of int | Float of float > > > > Yes, except for two facts: > > a) Each different type has different data available (some are ints, some > strings, some more complex) > b) Each different type has a lot of data. I don't really want to > specify a 10-member tuple for each constructor :) > c) Where the data is used, I want to match two or three different types, > and then call a default method with anything left over. With a union, I > have to deal with every possible type, or do nothing with the data. > > To clarify, using your type above, I can't do > > match n with > Int(i) -> print_int i > | _ -> print_int some_data_from_n > > Remember, all of my structures have many more than 1 piece of data. A > class inheritance really is the best way to model the data structure, I > just want to be able to pattern match on it. > > katre > You could wrap your items in a union: exception NoExternalRepForBase type 'a base_extern_t = Derived1 of 'a | Derived2 of 'a class virtual base = object method virtual as_external : unit -> base base_extern_t end class derived1 = object(self) inherit base method as_external () = Derived1 (self :> base) end class derived2 = object(self) inherit base method as_external () = Derived2 (self :> base) end let l = [ (new derived2 # as_external()) ; (new derived1 # as_external()) ] -- "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration." - S. Kelly-Bootle ------------------- 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