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 KAA17470; Thu, 13 Mar 2003 10:41:21 +0100 (MET) 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 KAA19341 for ; Thu, 13 Mar 2003 10:41:19 +0100 (MET) Received: from shiva.jussieu.fr (shiva.jussieu.fr [134.157.0.129]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h2D9fIX28896 for ; Thu, 13 Mar 2003 10:41:18 +0100 (MET) Received: from akasha.ijm.jussieu.fr (akasha.ijm.jussieu.fr [134.157.173.57]) by shiva.jussieu.fr (8.12.8/jtpda-5.4) with ESMTP id h2D9fHZC096264 ; Thu, 13 Mar 2003 10:41:17 +0100 (CET) Received: by akasha.ijm.jussieu.fr (Postfix, from userid 11004) id 7DD3D3CDC4; Thu, 13 Mar 2003 10:41:12 +0100 (CET) From: Olivier Andrieu Message-ID: <15984.21048.206090.793166@akasha.ijm.jussieu.fr> Date: Thu, 13 Mar 2003 10:41:12 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: Damien Cc: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] polymorphic methods In-Reply-To: <20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr> References: <20030313000716.3e6bb57c.Damien.Pous@ens-lyon.fr> <20030313105656R.garrigue@kurims.kyoto-u.ac.jp> <20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr> X-Mailer: VM 7.07 under Emacs 21.2.1 X-Antivirus: scanned by sophie at shiva.jussieu.fr X-Spam: no; 0.00; andrieu:01 caml-list:01 damien:01 coercions:01 val:01 adder:01 coerce:01 mutable:01 subtype:01 coercion:01 polymorphic:01 olivier:02 node:02 tree:02 unit:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Damien [Thursday 13 March 2003] : > My goal is to define a tree, whose node's type is a class > type a_t, containing a method for adding children. > but the child type can be any subtype of a_t > > * this method can safely be typed [a_t->unit], but this will require a > lot of coercions in the rest of the code... How about adding a coerce method in a_t ? That way, the coercion appears only in the class definition, not everytime you want to add a node. ,---- | class type a_t = object | method add : a_t -> unit | method children : a_t list | method coerce : a_t | end | | class a : a_t = | object (self) | val mutable children = [] | method add c = children <- c :: children | method children = children | method coerce = (self :> a_t) | end `---- and then : parent#add child#coerce or even : let adder p c = p#add c#coerce Hope this helps, -- Olivier ------------------- 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