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 GAA03160; Thu, 1 Apr 2004 06:00:23 +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 GAA02784 for ; Thu, 1 Apr 2004 06:00:21 +0200 (MET DST) From: briand@aracnet.com Received: from citrine.spiritone.com (citrine.spiritone.com [216.99.193.133]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3140JYM007634 for ; Thu, 1 Apr 2004 06:00:20 +0200 Received: from soggy.deldotd.com (216-99-206-32.cust.aracnet.com [216.99.206.32]) by citrine.spiritone.com (8.12.10/8.12.8) with ESMTP id i3145Uru015153; Wed, 31 Mar 2004 20:05:30 -0800 Received: from briand by soggy.deldotd.com with local (Exim 3.36 #1 (Debian)) id 1B8tN9-0000F8-00; Wed, 31 Mar 2004 20:00:07 -0800 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16491.37831.445677.611426@soggy.deldotd.com> Date: Wed, 31 Mar 2004 20:00:07 -0800 To: skaller@users.sourceforge.net Cc: Matt Gushee , caml-list@pauillac.inria.fr Subject: Re: [Caml-list] examples of heterogenous collections (containers ?) In-Reply-To: <1080723895.13269.143.camel@pelican.wigram> References: <16490.20296.852406.613011@soggy.deldotd.com> <20040331054113.GC19538@swordfish> <16490.26870.331855.463270@soggy.deldotd.com> <1080723895.13269.143.camel@pelican.wigram> X-Mailer: VM 6.92 under Emacs 21.2.1 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 abstraction:01 coercions:01 elt:01 elt:01 coerced:01 ocaml:01 int:01 int:01 writes:01 float:02 float:02 objects:02 objects:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 2 >>>>> "skaller" == skaller writes: skaller> You are thinking about this problem *backwards*! :) Well that's why it's giving me so much trouble. :-) skaller> The cast will always work if the object has a method named skaller> 'f' which when invoked with no arguments returns unit. skaller> Obviously, because Ocaml is wonderful, your cast will fail skaller> on objects without a suitable 'f' method. I LIKE IT. No inheritance required. Objects which have the same method with the same signature work "automagically". skaller> The solution is to use a factory function in a single skaller> module, and return an abstraction of the class type you skaller> wish to deal with. I'm not sure I followed that, can you expand ? For future readers of the list here and appropriate example. It turns out I was silly and never even bothered to look in the _manual_ of all places. I looked everywhere else. This is covered in the section called "Using Coercions", go figure. Anyway, enough blather, here's the example : class elt = object method f x = 2 * x end ;; class elt_A = object method f x = 3 *x method g x = 3. *. x end ;; let the_list = [ new elt; new elt ; ((new elt_A) :> elt) ] ;; List.iter (fun x -> print_int (x#f 2);) the_list ;; It does the right thing. Here is a nice example to show type safety. Re-define elt_A as: class elt_A = object method f x = 3. *. x method g x = 3. *. x end And get the error : This expression cannot be coerced to type elt = < f : int -> int >; it has type elt_A = < f : float -> float; g : float -> float > but is here used with type #elt as 'a = < f : int -> int; .. > Thanks to all who answered. Brian ------------------- 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