caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: David Powers <david@grayskies.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Looking for suggestions on self-referential object definitions
Date: Sun, 5 Mar 2006 00:21:19 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.63.0603042355470.5471@droopy> (raw)
In-Reply-To: <440A6D81.9020005@grayskies.net>

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


  parent reply	other threads:[~2006-03-05  8:23 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   ` Looking for suggestions on self-referential object definitions David Powers
2006-03-05  5:31     ` [Caml-list] " Jonathan Roewen
2006-03-05 14:37       ` David Powers
2006-03-05  8:21     ` Martin Jambon [this message]
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=Pine.LNX.4.63.0603042355470.5471@droopy \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=david@grayskies.net \
    /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).