Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Design advice
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: Re: [Caml-list] Design advice

I usually use variants, but it's not like I've been programming caml for 
much longer than you have.

>type, but now I have to convert this to an integer whenever I want to
>use the player number as an index.

Note that there is a correspondence between variant constructors with no 
arguments and integers 
(http://caml.inria.fr/ocaml/htmlman/manual032.html#htoc212), so:

type suit = Spades | Hearts | Diamonds | Clubs

let suit_to_int (s : suit) =
         assert (Obj.is_int (Obj.repr s));
         ((Obj.magic s) : int)

The assert will catch it if you add variables to one of the constructors 
and then call this.  This uses magic, which is bad, but it alleviates you 
having to type the variant constructors again and possibly make an error in 
the assocation with the int, which is good.  Pick your poison.

Chris


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners