From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 70741BC57 for ; Mon, 7 Jun 2010 20:48:45 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjQCAMnbDExRZ90wkWdsb2JhbACeIBUBAQEBCQsKBxEDH8BVglmCPgQ X-IronPort-AV: E=Sophos;i="4.53,379,1272837600"; d="scan'208";a="60819735" Received: from mtaout02-winn.ispmail.ntl.com ([81.103.221.48]) by mail1-smtp-roc.national.inria.fr with ESMTP; 07 Jun 2010 20:48:45 +0200 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20100607184844.OUJS3192.mtaout02-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com>; Mon, 7 Jun 2010 19:48:44 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20100607184840.FYIU1593.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com>; Mon, 7 Jun 2010 19:48:40 +0100 Received: from Tenor ([172.16.0.8]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id o57ImaFo000738 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Mon, 7 Jun 2010 19:48:37 +0100 From: "David Allsopp" To: "=?iso-8859-1?Q?'T=F6r=F6k_Edwin'?=" , References: <4C0D3557.1020200@gmail.com> In-Reply-To: <4C0D3557.1020200@gmail.com> Subject: RE: [Caml-list] Converting variants with only constant constructors to integers Date: Mon, 7 Jun 2010 19:48:36 +0100 Message-ID: <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQK3OCAoaAveGworypJxQY6jcfYL4QG5R1OK Content-Language: en-gb Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=W3tOLUehizD4qj6VhtReFuw5MKb8d+XqjIxlDsIazEA= c=1 sm=0 a=dfm47NNIjPMA:10 a=VZGMvYHv9TIA:10 a=8sW17jYxVu0A:10 a=8nJEP1OIZ-IA:10 a=14V3v33eA-XYzG-HZoQA:9 a=WLgaJ0mK_IGMmMksakv17T2-XEQA:4 a=wPNLvfGTeEIA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 X-Spam: no; 0.00; variants:01 constructors:01 integers:01 constructors:01 foo:01 foo:01 ocaml:01 variants:01 ocaml:01 bindings:01 edwin:98 wrote:01 integer:01 integer:01 caml-list:01 T=F6r=F6k Edwin wrote: > What is the recommended way to convert a variant that has only = constant > constructors to an integer? (an integer that is unique for each = constant > constructor, preferably sequential in the order in which they are > declared). type foo =3D A | B;; external int_of_foo : foo -> int =3D "%identity";; Which is another way of saying Obj.magic - but %identity is an OCaml primitive, where Obj.magic is part of a restricted module so there is a small semantic difference in terms of future support. The internal representation of variants won't change until at least OCaml 4.0.0 - there're simply far too many C bindings out there! foo_of_int obviously can't be done quite so trivially written although having checked that = the int represents a valid constructor you can then use %identity in the = same way. David=20