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 BAA15764; Tue, 25 Jun 2002 01:53:39 +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 BAA15791 for ; Tue, 25 Jun 2002 01:53:38 +0200 (MET DST) Received: from wetware.wetware.com (wetware.wetware.com [199.108.16.1]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g5ONraH24152 for ; Tue, 25 Jun 2002 01:53:37 +0200 (MET DST) Received: from (local broken client) localhost(dh01.wetware.com[199.108.16.41]) (1878 bytes) by wetware.wetware.com via sendmail with P:esmtp/R:bind_hosts/T:inet_zone_bind_smtp (sender: ) id for ; Mon, 24 Jun 2002 16:53:32 -0700 (PDT) (Smail-3.2.0.114 2001-Aug-6 #1 built 2002-Jan-4) Date: Mon, 24 Jun 2002 16:55:45 -0700 Subject: Re: [Caml-list] Recursive classes are impossible? Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v482) Cc: The Trade To: Gerd Stolpmann From: james woodyatt In-Reply-To: <20020624233419.C760@ice.gerd-stolpmann.de> Message-Id: Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.482) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Monday, June 24, 2002, at 02:34 PM, Gerd Stolpmann wrote: > > No, this is not the problem. Try the following: > > class tree = > object(self:'self) > val mutable v = (None : int option) > val mutable ch = ([] : 'self list) > method get = v > method set x = v <- x > method children = ch > method set_children x = ch <- x > end > > This definition works at the first glance, but if you try to > inherit from this class and subtype this class at the same > time, you will run into problems. > > class broccoli = > object > inherit tree > method color = "green" > end Here's a more graphic explanation of why this is a bad idea: let t = new tree;; let b = new broccoli;; (b :> tree)#set_children [t];; Here, the compiler-- quite sensibly-- errors on the coercion. The "set_children" method of class tree takes a list of tree objects. The "set_children" method of class broccoli takes a list of broccoli objects. If you were able to coerce a broccoli object into a tree object, you could then set its children to a list of non-broccoli tree objects-- which would violate the invariant for objects of the broccoli class. -- j h woodyatt ------------------- 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