From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 2DDB8BBBC for ; Sun, 5 Mar 2006 15:37:25 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k25EbOkB028205 for ; Sun, 5 Mar 2006 15:37:24 +0100 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 PAA27734 for ; Sun, 5 Mar 2006 15:37:23 +0100 (MET) Received: from mail1.grayskies.net (wrath.grayskies.net [151.202.218.61]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k25EbMKq028201 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 5 Mar 2006 15:37:23 +0100 Received: from [192.168.0.112] by mail1.grayskies.net with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.60 (FreeBSD)) (envelope-from ) id 1FFuOD-000BNv-U8; Sun, 05 Mar 2006 09:39:18 -0500 Message-ID: <440AF79E.1000407@grayskies.net> Date: Sun, 05 Mar 2006 09:37:18 -0500 From: David Powers User-Agent: Mail/News 1.5 (Macintosh/20051109) MIME-Version: 1.0 To: Jonathan Roewen Cc: caml-list@inria.fr Subject: Re: [Caml-list] Looking for suggestions on self-referential object definitions References: <20060304143608.GA16996@ours.starynkevitch.net> <440A6D81.9020005@grayskies.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 440AF7A4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 440AF7A2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 variants:01 val:01 mutable:01 val:01 mutable:01 printf:01 sprintf:01 printf:01 sprintf:01 recursive:01 weapon:98 weapon:98 .....:98 .....:98 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 I had no idea you could reference types in variant definitions before the types were fully defined - mostly due to some failure at trying to define the type before I even opened the class definition for container. That said, obviously you can, which is enormously helpful. ;) I assume (based on some simple experiments) that this will only work out using polymorphic variants. -David Jonathan Roewen wrote: >> class virtual item = >> object (self) >> val mutable name = "" >> >> method name = name >> >> method set_name newname = name <- newname >> end >> ;; >> >> >> class weapon = >> object (self) >> inherit item >> end >> ;; >> >> >> class container = >> object (self) >> inherit item >> >> val mutable items = [] >> >> method add newitem = items <- (newitem :: items) >> >> method contents = items >> >> method remove i = items <- List.filter (fun x -> x != i) items >> >> method contents_to_string = >> let print_item i = >> match i with >> | `Weapon w -> Printf.sprintf "%s (weapon)" w#name >> | `Container c -> Printf.sprintf "%s (container) - >> Containing:\n%s" c#name c#contents_to_string >> in >> List.map print_item items >> >> end >> ;; > > First off, method contents_to_string has conflicting types to be > recursive. Second, adding a type constraint to items should fix your > problems. > > My changes: > > class container = > ..... > val mutable items : [ `Weapon of weapon | `Container of container > ] list = [] > ... > method contents_to_string = > ..... > List.fold_left (fun a b -> if a = "" then print_item b else a > ^ "; " ^ print_item b) "" items > > end > ;; > > Jonathan