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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id E3CBDBBBB for ; Sun, 5 Mar 2006 05:48:07 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k254m7UX027256 for ; Sun, 5 Mar 2006 05:48:07 +0100 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 FAA19197 for ; Sun, 5 Mar 2006 05:48:06 +0100 (MET) Received: from mail1.grayskies.net (wrath.grayskies.net [151.202.218.61]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k254m5YK027251 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 5 Mar 2006 05:48:06 +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 1FFlBn-000G52-G3 for caml-list@inria.fr; Sat, 04 Mar 2006 23:49:51 -0500 Message-ID: <440A6D81.9020005@grayskies.net> Date: Sat, 04 Mar 2006 23:48:01 -0500 From: David Powers User-Agent: Mail/News 1.5 (Macintosh/20051109) MIME-Version: 1.0 To: caml-list@inria.fr Subject: Looking for suggestions on self-referential object definitions References: <20060304143608.GA16996@ours.starynkevitch.net> In-Reply-To: <20060304143608.GA16996@ours.starynkevitch.net> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 440A6D87.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 440A6D85.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ocaml:01 model:01 model:01 val:01 mutable:01 val:01 mutable:01 printf:01 sprintf:01 printf:01 sprintf:01 weapon:98 weapon:98 inherit:01 inherit: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.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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 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 ;;