From: David Powers <david@grayskies.net>
To: Jonathan Roewen <jonathan.roewen@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Looking for suggestions on self-referential object definitions
Date: Sun, 05 Mar 2006 09:37:18 -0500 [thread overview]
Message-ID: <440AF79E.1000407@grayskies.net> (raw)
In-Reply-To: <ad8cfe7e0603042131q3ab6af1focdb6fc8272e4c0a5@mail.gmail.com>
I had no idea you could reference types in variant definitions before
the types were fully defined - mostly due to some failure at trying to
define the type before I even opened the class definition for container.
That said, obviously you can, which is enormously helpful. ;)
I assume (based on some simple experiments) that this will only work out
using polymorphic variants.
-David
Jonathan Roewen wrote:
>> 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
next prev parent reply other threads:[~2006-03-05 14:37 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 [this message]
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=440AF79E.1000407@grayskies.net \
--to=david@grayskies.net \
--cc=caml-list@inria.fr \
--cc=jonathan.roewen@gmail.com \
/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