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 RAA09559; Sat, 20 Sep 2003 17:05:10 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 RAA24711 for ; Sat, 20 Sep 2003 17:05:09 +0200 (MET DST) Received: from ep09.kernel.pl (ep09.kernel.pl [212.87.11.162]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h8KF57H17304 for ; Sat, 20 Sep 2003 17:05:07 +0200 (MET DST) Received: (qmail 24183 invoked by uid 566); 20 Sep 2003 15:05:04 -0000 Date: Sat, 20 Sep 2003 17:00:41 +0200 From: Michal Moskal To: katre Cc: caml-list@inria.fr Subject: Re: [Caml-list] Subclasses and pattern matching Message-ID: <20030920150041.GA4171@roke.freak> Mail-Followup-To: katre , caml-list@inria.fr References: <20030919142156.GA30187@henchmonkey.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030919142156.GA30187@henchmonkey.org> User-Agent: Mutt/1.4.1i X-PGP-Fingerprint: CF89 1B14 11BE 1CC9 2CA3 7497 5E32 69B4 BC71 B4C2 X-AntiVirus: scanned for viruses by AMaViS 0.2.1 (http://amavis.org/) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; michal:01 moskal:01 malekith:01 pld-linux:01 caml-list:01 0400,:01 runtime:01 michal:01 moskal:01 malekith:01 kernel:01 ocaml:01 inherit:01 inherit:01 sep:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Fri, Sep 19, 2003 at 10:21:56AM -0400, katre wrote: > Hello again! > > I'm working on a project where I want to create a list of data, which is > all similar but not identical. In other languages with which I am > familiar, I would do this with a base class and a few subclasses, as > such: > > class virtual abstractClass = > object > > method virtual to_string : unit -> string > end > > class subclass1 = > object > inherit abstractClass > > method thing () = "subclass1 thing" > method to_string () = "subclass 1" > end > > class subclass2 = > object > inherit abstractClass > > method thing2 () = "subclass2 thing" > method to_string () = "subclass 2" > end > > And I would create the list as > > let l = [ (new subclass1 :> abstractClass); (new subclass :> abstractClass); (new subclass1 :> abstractClass); (new subclass2 :> abstractClass) ]; > > This seems to work fine. The best way that would allow _ -> ... thing is something like this: type specific = Subclass1 of subclass1 | Subclass2 of subclass2 | ... | SubclassN of subclassN type data = abstractClass * specific match l with | (_, Subclass1 x) :: xs -> ... | (_, Subclass2 x) :: xs -> ... | (x, _) :: xs -> ... In general there are no runtime types in OCaml, you have to organize them yourself. -- : Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv : When in doubt, use brute force. -- Ken Thompson : {E-,w}-- {b++,e}>+++ h ------------------- 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