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 HAA14102; Wed, 31 Mar 2004 07:41:16 +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 HAA14306 for ; Wed, 31 Mar 2004 07:41:15 +0200 (MET DST) Received: from mz3.forethought.net (mzpi5.forethought.net [216.241.36.14]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i2V5fvjq015897 for ; Wed, 31 Mar 2004 07:41:58 +0200 Received: from [216.241.35.41] (helo=swordfish) by mz3.forethought.net with esmtp (Exim 4.30) id 1B8YTQ-00047f-83 for caml-list@pauillac.inria.fr; Tue, 30 Mar 2004 22:41:12 -0700 Received: from matt by swordfish with local (Exim 3.35 #1 (Debian)) id 1B8YTS-0007Q4-00 for ; Tue, 30 Mar 2004 22:41:14 -0700 Date: Tue, 30 Mar 2004 22:41:14 -0700 From: Matt Gushee To: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] examples of heterogenous collections (containers ?) Message-ID: <20040331054113.GC19538@swordfish> Reply-To: Matt Gushee Mail-Followup-To: caml-list@pauillac.inria.fr References: <16490.20296.852406.613011@soggy.deldotd.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <16490.20296.852406.613011@soggy.deldotd.com> User-Agent: Mutt/1.3.27i X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; gushee:01 mgushee:01 havenrock:01 caml-list:01 2004:99 objs:01 non-trivial:01 subtyping:01 arbitrarily:01 casts:01 gui:01 gushee:01 englewood:01 manure:01 mgushee:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, Mar 30, 2004 at 08:55:36PM -0800, briand@aracnet.com wrote: > > I'm embarking on that most typical of ewexamples, the heterogenous list. > > So I have objects: > > base_class > virtual method > > derived_class_A > inherit base_class > > derived_class_B > inherit base_class > > ... > > And I would like to collect instances of derived_class_B, > derived_class_B etc.. into a data structure. > > The obvious example is that I'd like to collect the objects into a > list and do something like : > > List.iter > (f obj -> > obj#method) > list_of_objs This will work if all the classes have identical signatures. Of course, that's not the case for most non-trivial examples. > or is (obj :> base_class)#method ? Probably not. First of all, you will need to cast the objects at the point where you put them into a list together ... thus, at the point where you do the method call, the cast has already been done. Second, you don't necessarily want to cast them to the base class. As some people never tire of pointing out ;-), inheritance is not subtyping. Actually, I haven't myself figured out all the implications of that statement, nor learned the theoretical justification for it--but at any rate, in OCaml class types are completely independent of inheritance relationships. That's a good thing because it means you can define a class type and arbitrarily apply it to any class that provides *at least* the methods specified in the type signature, regardless of inheritance. E.g.: class type x = object method a : unit method b : int -> bool end ... and so on. > I've done quite a bit of searching but I cannot seem to find a "clean" > example of doing the above, just bits and pieces. Can anyone point me > to any examples or code ? Hmm ... I recently wrote a text editor that uses a lot of class casts in order to be able to manipulate groups of GUI objects that are instances of different classes. I'm not sure it's a great learning example, though. It works, but its code is hardly clean ... in fact it's easily the ugliest thing I've done in OCaml. But if you can't find any better examples, I could send you a tarball of it (it's not released yet). -- Matt Gushee When a nation follows the Way, Englewood, Colorado, USA Horses bear manure through mgushee@havenrock.com its fields; http://www.havenrock.com/ When a nation ignores the Way, Horses bear soldiers through its streets. --Lao Tzu (Peter Merel, trans.) ------------------- 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