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 25A0BBB81 for ; Sun, 5 Mar 2006 09:23:36 +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 k258NZFi000678 for ; Sun, 5 Mar 2006 09:23:35 +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 JAA21869 for ; Sun, 5 Mar 2006 09:23:34 +0100 (MET) Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [66.111.4.28]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k258NXIO000675 for ; Sun, 5 Mar 2006 09:23:34 +0100 Received: from frontend1.internal (mysql-sessions.internal [10.202.2.149]) by frontend1.messagingengine.com (Postfix) with ESMTP id 1C048D38A32; Sun, 5 Mar 2006 03:23:33 -0500 (EST) Received: from frontend2.messagingengine.com ([10.202.2.151]) by frontend1.internal (MEProxy); Sun, 05 Mar 2006 03:23:33 -0500 X-Sasl-enc: K7Prpp0J8pbxVQkUS7jxhQq0d20f9IpAxExUkZpniG/w 1141547011 Received: from [172.16.13.149] (burnham.ljcrf.edu [192.231.106.2]) by frontend2.messagingengine.com (Postfix) with ESMTP id 0C82858A118; Sun, 5 Mar 2006 03:23:29 -0500 (EST) Date: Sun, 5 Mar 2006 00:21:19 -0800 (PST) From: Martin Jambon X-X-Sender: martin@droopy To: David Powers Cc: caml-list@inria.fr Subject: Re: [Caml-list] Looking for suggestions on self-referential object definitions In-Reply-To: <440A6D81.9020005@grayskies.net> Message-ID: References: <20060304143608.GA16996@ours.starynkevitch.net> <440A6D81.9020005@grayskies.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at nez-perce with ID 440AA007.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 440AA005.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 model:01 model:01 compiler:01 val:01 mutable:01 val:01 mutable:01 printf:01 sprintf:01 printf:01 sprintf:01 recursive:01 compiler:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 On Sat, 4 Mar 2006, David Powers wrote: > I am in the middle of hacking together a rogue-like game in OCaml for fun and > to get a better feel for the language, and I have come across a stumbling > block. Specifically I began to model items in the game as objects deriving > from a base item class. All well and good until I tried to come up with a > way to model a container (like a backpack, or sack). The container itself > was an item, that could hold other items - including, possibly, other > containers. > > Some brief dabbling led me to the idea that I could store the items in the > container in a list using a variant type to differentiate the specific types > of items - but I can't for the life of me think how to add containers to that > type list without having defined containers first. I've included the simple > code below so that hopefully some smart person can point out how dumb I'm > being. ;) > > -David What you are doing is correct, you just need to tell the compiler about the type of the items. There are a several ways of doing this, here is one: 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 ;; type 'a item = [ `Container of 'a | `Weapon of weapon ] class container = object (self) inherit item val mutable items : container item list = [] 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 String.concat "\n" (List.map print_item items) end ;; Another possibility is to define the type of the objects without defining a class or class type, so that you can write directly a recursive type definition separately from the class definition: type obj = < content : item list > and item = [ `A | `B of obj ] (* BTW I don't know how to tell the compiler that the class creates objects of type obj, but this can be done: *) class c = object method content : item list = [] end Martin -- Martin Jambon, PhD http://martin.jambon.free.fr Visit http://wikiomics.org, bioinformatics wiki