From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 033ADBC57 for ; Mon, 7 Jun 2010 20:07:24 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmYCACLSDEzRVaE0mGdsb2JhbACSDowKCBUBAQEBAQgJDAcRIq9+ggGFTy6ITwEBAwWFEgSDRQ X-IronPort-AV: E=Sophos;i="4.53,379,1272837600"; d="scan'208";a="52614869" Received: from mail-fx0-f52.google.com ([209.85.161.52]) by mail2-smtp-roc.national.inria.fr with ESMTP; 07 Jun 2010 20:07:23 +0200 Received: by fxm19 with SMTP id 19so1931746fxm.39 for ; Mon, 07 Jun 2010 11:07:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:received:message-id:date:from :user-agent:mime-version:to:subject:x-enigmail-version:openpgp :content-type:content-transfer-encoding; bh=w0ymQOcI2FjN73Z4AwecuE4Znh26lS+HMHoAdIS6pow=; b=sV4ZUE7lSSElF8bBkFepthDUAI8+aiTeVQJe4HNkzOfsFA+EJWEcGMmmeeFIIxF7Ta j4mDR72MpBkQyTn6J8gDu1/iiwFPXnZmxLzFQgg+EvYwqhEtnKxtdpNZheyPun4Y2Sh5 1t0ye19olJZUW/+dI0bfTdw4oMjjq8wi8lASs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :x-enigmail-version:openpgp:content-type:content-transfer-encoding; b=uBBkB+UfApTk1+rQPtaifMzhCwwLv1QxxXH/qW+KN7+fCGKCIuNQFhZkiPcU8FdVRw MV9wn0+XzUTjvzp2G3bVeEcv9ainUdFN8/UC3pS6JExwXdUWGlpjdA0hjJ69pfGBpgRM nXi/CjAtuZqOxoFJOqCExKo+9YGMJpiIFGFVU= Received: by 10.103.76.26 with SMTP id d26mr5031080mul.115.1275934041533; Mon, 07 Jun 2010 11:07:21 -0700 (PDT) Received: from debian ([79.114.32.39]) by mx.google.com with ESMTPS id g1sm6413237muf.0.2010.06.07.11.07.20 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 07 Jun 2010 11:07:20 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) by debian (Postfix) with ESMTP id E921F12F030 for ; Mon, 7 Jun 2010 21:07:19 +0300 (EEST) Message-ID: <4C0D3557.1020200@gmail.com> Date: Mon, 07 Jun 2010 21:07:19 +0300 From: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100515 Icedove/3.0.4 MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Converting variants with only constant constructors to integers X-Enigmail-Version: 1.0.1 OpenPGP: id=5379965D Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; variants:01 constructors:01 integers:01 constructors:01 ocaml:01 hashtbl:01 hash:01 edwin:98 integer:01 integer:01 assert:01 functions:01 constructor:01 int:01 int:01 Hi, 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). I found the following two possibilities, but I am not sure if these are guaranteed to return the same results for future versions of OCaml or not: let int_of_constant_variant a : int = Hashtbl.hash a;; let int_of_constant_variant a : int = let r = Obj.repr a in assert (Obj.is_int r); (Obj.magic r : int);; for 'type t = A | C | B', both of these functions return 0,1, and 2 for A,C, and B respectively. Best regards, --Edwin