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 LAA28901; Wed, 31 Mar 2004 11:05:15 +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 LAA28967 for ; Wed, 31 Mar 2004 11:05:13 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2V95AYM015859 for ; Wed, 31 Mar 2004 11:05:12 +0200 Received: from [192.168.1.200] (ppp113-158.lns1.syd3.internode.on.net [150.101.113.158]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i2V94twn049502; Wed, 31 Mar 2004 18:34:56 +0930 (CST) Subject: Re: [Caml-list] examples of heterogenous collections (containers ?) From: skaller Reply-To: skaller@users.sourceforge.net To: briand@aracnet.com Cc: Matt Gushee , caml-list@pauillac.inria.fr In-Reply-To: <16490.26870.331855.463270@soggy.deldotd.com> References: <16490.20296.852406.613011@soggy.deldotd.com> <20040331054113.GC19538@swordfish> <16490.26870.331855.463270@soggy.deldotd.com> Content-Type: text/plain Message-Id: <1080723895.13269.143.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 31 Mar 2004 19:04:55 +1000 Content-Transfer-Encoding: 7bit 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 sourceforge:01 2004:99 elt:01 list':01 elt:01 val:01 abstraction:01 9660:01 glebe:01 ocaml:01 interfaces:01 nsw:01 snail:02 constructs:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 2004-03-31 at 16:45, briand@aracnet.com wrote: > So it seems to me that I need a "stronger" method to enforce safety, > perhaps implementing x1 as a virtual method in base ? You are thinking about this problem *backwards*! :) Forget about how to declare the classes etc. Focus on the list. You would like a list of some type "elt" for which you can write: List.iter (fun x -> x#f) the_list; Now define the type which 'the_list' must have. Define the 'minimal' such type! class type elt = object method f : unit end val the_list : elt list That's it. All you need to do now is cast ANY object to type "elt" and it will fit into your list. The cast will always work if the object has a method named 'f' which when invoked with no arguments returns unit. Obviously, because Ocaml is wonderful, your cast will fail on objects without a suitable 'f' method. The KEY idea here is: the class types you need for algorithms should be declared. But forget the idea classes have types. They do, but it isn't important. A class is just a function which creates an object. Class types are *views* of objects. The class type of a class is the 'maximal' view of an object which it constructs, and isn't really that important, in fact, it should probably never be exposed in any interfaces because the full class type will include data members due to a problem needing to allocate space for them when constructing an object. The solution is to use a factory function in a single module, and return an abstraction of the class type you wish to deal with. -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- 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