Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2002-09-28 (10:55)
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 
(, 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.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: