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 JAA22116; Wed, 31 Mar 2004 09:28:36 +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 JAA21636 for ; Wed, 31 Mar 2004 09:28:34 +0200 (MET DST) Received: from out2.smtp.messagingengine.com (out2.smtp.messagingengine.com [66.111.4.26]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2V7SXYM000584 for ; Wed, 31 Mar 2004 09:28:34 +0200 X-Sasl-enc: 1F1HEVI0vz4BoU4co9v/5g 1080718111 Received: from [192.168.1.100] (unknown [218.81.120.251]) by mail.messagingengine.com (Postfix) with ESMTP id D33F98DC742; Wed, 31 Mar 2004 02:28:30 -0500 (EST) Date: Wed, 31 Mar 2004 15:28:13 +0800 (HKT) From: Martin Jambon X-X-Sender: martin@localhost To: Matt Gushee Cc: caml-list@inria.fr Subject: Re: [Caml-list] examples of heterogenous collections (containers ?) In-Reply-To: <20040331054113.GC19538@swordfish> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 gushee:01 2004:99 objs:01 non-trivial:01 inherit:01 inherit:01 variants:01 variants:01 polymorphic:01 signatures:02 variant:02 variant:02 objects:02 objects:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, 30 Mar 2004, Matt Gushee wrote: > 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. And to keep trace of the original object, we would use polymorphic variants: class virtual ['a] base_class = object method virtual variant : 'a end class ['a] derived_class_A = object (self) inherit ['a] base_class method variant = `A self end and ['a] derived_class_B = object (self) inherit ['a] base_class method variant = `B self end type variant = [ `A of variant derived_class_A | `B of variant derived_class_B ] let a = new derived_class_A let b = new derived_class_B let l = [ (a :> variant base_class); (b :> variant base_class) ] let list_of_variants = List.map (fun o -> o#variant) l But maybe it is more convenient to not define a variant method, just build a list of specialized objects: let list_of_variants = [ `A obj1; `B obj2; `C anything; ... ] and then: let l = List.map (function `A o | `B o | `C o -> (o :> base_class)) (but here you loose the reference to the specialized version) Martin ------------------- 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