From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA09075; Sat, 28 Sep 2002 21:02:22 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA09006 for ; Sat, 28 Sep 2002 21:02:21 +0200 (MET DST) Received: from nexus.stwing.upenn.edu (NEXUS.STWING.UPENN.EDU [165.123.132.61]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g8SJ2K529800 for ; Sat, 28 Sep 2002 21:02:20 +0200 (MET DST) Received: from force.stwing.upenn.edu (daemon@force.stwing.upenn.edu [165.123.132.65]) by nexus.stwing.upenn.edu (8.11.6/8.11.6) with ESMTP id g8SJ2IK03082 (using TLSv1/SSLv3 with cipher EDH-RSA-DES-CBC3-SHA (168 bits) verified NO) for ; Sat, 28 Sep 2002 15:02:19 -0400 (EDT) Received: (from wlovas@localhost) by force.stwing.upenn.edu (8.11.6/8.11.6) id g8SJ2HP10059 for caml-list@inria.fr; Sat, 28 Sep 2002 15:02:17 -0400 (EDT) Date: Sat, 28 Sep 2002 15:02:17 -0400 From: William Lovas To: caml-list@inria.fr Subject: Re: [Caml-list] Design advice Message-ID: <20020928190216.GA9876@force.stwing.upenn.edu> Mail-Followup-To: caml-list@inria.fr References: <4.3.2.7.2.20020928033941.03b2eb10@mail.d6.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4.3.2.7.2.20020928033941.03b2eb10@mail.d6.com> User-Agent: Mutt/1.4i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, Sep 28, 2002 at 03:55:43AM -0700, Chris Hecker wrote: > >type, but now I have to convert this to an integer whenever I want to > >use the player number as an index. > > 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. Wouldn't it be safer to just do something like: let suit_to_int = function | Spades -> 0 | Hearts -> 1 | Diamonds -> 2 | Clubs -> 3 ? You only have to type it once, and if you change the constructors around, it won't even compile. Plus it gives you control over which int a suit corresponds to -- i doubt you can get any sort of guarantee if you use Obj.magic. William ------------------- 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