From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 4C067BC57 for ; Mon, 7 Jun 2010 20:26:25 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkICANPWDExKfVI0mGdsb2JhbACDHZp7CBUBAQEBAQgJDAcRIq9GgjoBBY5IAQSBJoMDbg X-IronPort-AV: E=Sophos;i="4.53,379,1272837600"; d="scan'208";a="51923491" Received: from mail-ww0-f52.google.com ([74.125.82.52]) by mail3-smtp-sop.national.inria.fr with ESMTP; 07 Jun 2010 20:26:24 +0200 Received: by wwb31 with SMTP id 31so3038686wwb.39 for ; Mon, 07 Jun 2010 11:26:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:references :date:in-reply-to:message-id:user-agent:mime-version:content-type :content-transfer-encoding; bh=JJcfxlGTnluBOfDHdjhdr7EdiYjRwyEKJZ66cBZFtE4=; b=OTaSq9AMLX8qtvb49xh9b4dR7IoOt870f93UQJEdDGWxFyALpKxXt6zH5eo+FjZI/y Bm4GWXypd8ks1eaV1ZUmNF1bWKFA3O2rifVC0b/FKQrKrFnslnoEBX12t0hdFpA4Jwpf OvYqBRvxVeRUE8J/UJyUhskpJExoxl6hLDLt4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type:content-transfer-encoding; b=kEr7VrytPPTs3IBl06zXmdHl5PDJSuhOuAiI7Ysdjjkbec6KntLlBMsIp1Oo1SNigd Ehi/HGvW6ILDXsvEKyfAN6OQsIaydM2hN7TvMR1PuO5p5QucE7ZOaKWiqShXVXbifejJ GChmQfnLjdRY6A/Ah3k+ELZrTMPHlK6ew+5AQ= Received: by 10.227.127.83 with SMTP id f19mr14067578wbs.83.1275935184353; Mon, 07 Jun 2010 11:26:24 -0700 (PDT) Received: from spec-desktop.specuu.com (host86-141-180-78.range86-141.btcentralplus.com [86.141.180.78]) by mx.google.com with ESMTPS id u36sm39723743wbv.0.2010.06.07.11.26.19 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 07 Jun 2010 11:26:19 -0700 (PDT) From: W Dan Meyer To: =?utf-8?B?VMO2csO2aw==?= Edwin Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Converting variants with only constant constructors to integers References: <4C0D3557.1020200@gmail.com> Date: Mon, 07 Jun 2010 19:25:55 +0100 In-Reply-To: <4C0D3557.1020200@gmail.com> (=?utf-8?B?IlTDtnLDtms=?= Edwin"'s message of "Mon, 07 Jun 2010 21:07:19 +0300") Message-ID: <87ocfm7l8s.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; variants:01 constructors:01 integers:01 constructors:01 stdlib:01 camlp:01 ocaml:01 hashtbl:01 hash:01 casts:01 marshaling:01 edwin:98 integer:01 integer:01 assert:01 T=C3=B6r=C3=B6k Edwin writes: > 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 declare= d). In the first play you'd need to tell us, why would you need this, and then maybe there are other solutions to your problem. Most likely Marshal module from stdlib, will full-fill your requirements. I can imagine you could use an association list, function with pattern matching, code generation tool (based on CamlP4), etc. The later would be needed if you have varied data structure, that change frequently or it is too big to maintain it with just pattern matching. > > 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 =3D Hashtbl.hash a;; > let int_of_constant_variant a : int =3D > let r =3D Obj.repr a in > assert (Obj.is_int r); > (Obj.magic r : int);; There is no guarantee of these kind of unsafe casts at all (means using Obj.magic). Especially Obj.magic should not be used in marshaling, but most common patter is for type system tricks. Wojciech