caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Powers <david@grayskies.net>
To: caml-list@inria.fr
Subject: Looking for suggestions on self-referential object definitions
Date: Sat, 04 Mar 2006 23:48:01 -0500	[thread overview]
Message-ID: <440A6D81.9020005@grayskies.net> (raw)
In-Reply-To: <20060304143608.GA16996@ours.starynkevitch.net>

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
;;


  parent reply	other threads:[~2006-03-05  4:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-04 14:04 Benchmarks against imperative languages Sarah Mount
2006-03-04 14:36 ` [Caml-list] " Basile STARYNKEVITCH
2006-03-04 18:01   ` David Teller
2006-03-05  9:38     ` Richard Jones
2006-03-05 14:38       ` Christophe TROESTLER
2006-03-05  4:48   ` David Powers [this message]
2006-03-05  5:31     ` [Caml-list] Looking for suggestions on self-referential object definitions Jonathan Roewen
2006-03-05 14:37       ` David Powers
2006-03-05  8:21     ` Martin Jambon
2006-03-05 15:16     ` Oliver Bandel
2006-03-05 11:54   ` [Caml-list] Benchmarks against imperative languages Jon Harrop
2006-03-05 13:20     ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=440A6D81.9020005@grayskies.net \
    --to=david@grayskies.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).