caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
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 18:31:53 +1300	[thread overview]
Message-ID: <ad8cfe7e0603042131q3ab6af1focdb6fc8272e4c0a5@mail.gmail.com> (raw)
In-Reply-To: <440A6D81.9020005@grayskies.net>

> 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


  reply	other threads:[~2006-03-05  9:30 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     ` Jonathan Roewen [this message]
2006-03-05 14:37       ` [Caml-list] " 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=ad8cfe7e0603042131q3ab6af1focdb6fc8272e4c0a5@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --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).