From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id E7A09BC57 for ; Mon, 7 Jun 2010 20:32:33 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkICAP7XDEzRVaE0mGdsb2JhbACDHZp7CBUBAQEBAQgJDAcRIq9NOYIBhVUuiE8BAQMFgSGDA24Eg0U X-IronPort-AV: E=Sophos;i="4.53,380,1272837600"; d="scan'208";a="64130482" Received: from mail-fx0-f52.google.com ([209.85.161.52]) by mail4-smtp-sop.national.inria.fr with ESMTP; 07 Jun 2010 20:32:33 +0200 Received: by fxm19 with SMTP id 19so1953344fxm.39 for ; Mon, 07 Jun 2010 11:32:32 -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:cc:subject:references:in-reply-to :x-enigmail-version:openpgp:content-type:content-transfer-encoding; bh=pBxwRnqZXhCSBNJ7d2qhtjAE3dWlYbXaUCt7yr8/wjQ=; b=MH345rohysReduKGydNK7c1Cv4gK1BX6qTLPcuQBCLV7wzJBGbeWrtIViK61L2ADS+ MMDLhcCRQP6IoMhvYkjvSMDQy6GPlrdbGT9790OL4DG8fzdZGQaATz/uufepO4v9t8J7 +9ozsfvic30HaHdBPDlxZ19fuVx1tSyOKZREI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:x-enigmail-version:openpgp:content-type :content-transfer-encoding; b=gqXqunHiklv1IwD45Yd1oXXFgvYPAnayW1kkOYb/d4WcW7YfvtzPbyA4XgBO5CO0oX 7gLlFOp+JY4ep5UfuPEtmi7PEbqrPHC+QKFsYHKAINyWMlgCDRmQRmsPsfK0W2OAOjN6 an/f899krDZdGVPCDZus66LzlZ9HlGy4cesSM= Received: by 10.223.66.21 with SMTP id l21mr15503725fai.90.1275935552866; Mon, 07 Jun 2010 11:32:32 -0700 (PDT) Received: from debian ([79.114.32.39]) by mx.google.com with ESMTPS id z12sm21424559fah.21.2010.06.07.11.32.31 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 07 Jun 2010 11:32:31 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) by debian (Postfix) with ESMTP id DF0E012F030; Mon, 7 Jun 2010 21:32:30 +0300 (EEST) Message-ID: <4C0D3B3E.5050408@gmail.com> Date: Mon, 07 Jun 2010 21:32:30 +0300 From: =?UTF-8?B?VMO2csO2ayBFZHdpbg==?= 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: Wojciech Meyer Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Converting variants with only constant constructors to integers References: <4C0D3557.1020200@gmail.com> <87sk4y7lc7.fsf@gmail.com> <4C0D3B0F.4060502@gmail.com> In-Reply-To: <4C0D3B0F.4060502@gmail.com> X-Enigmail-Version: 1.0.1 OpenPGP: id=5379965D Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; variants:01 constructors:01 integers:01 constructors:01 flags:01 enum:01 ocaml:01 flags:01 ocaml:01 enum:01 stdlib:01 camlp:01 hashtbl:01 hash:01 casts:01 On 06/07/2010 09:31 PM, Török Edwin wrote: > On 06/07/2010 09:23 PM, Wojciech Meyer wrote: >> Török 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 declared). >> >> In the first play you'd need to tell us, why would you need this > > Yes, I probably should have started with that. > I have some flags like: > type myflags = Property1 | Property2 | Property3;; > > I have something similar code in C, as an enum. > I need to convert to convert OCaml lists of flags to an integer, that > will ultimately be passed to a C function. > > So [Property1; Property3] needs to become (1 << Property1) | (1 << > Property3). I want to do this conversion in OCaml code. > > Important is that "Property1" from OCaml gets the same value as > Property1 in the C enum. > >> , and >> then maybe there are other solutions to your problem. >> Most likely Marshal module from stdlib, will full-fill your requirements. > > If I only had OCaml code to deal with yes, but see above. > >> >> I can imagine you could use an association list, function with pattern >> matching > > I don't have too many flags, so I could write a simple pattern matching > function to convert to int (even manually). > I just thought there is a way to do this easily, and more general than that. > >> , 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 = 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);; >> >> 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. > > How about the hash value? I guess no guarantees about that either. > > Best regards, > --Edwin