Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Török Edwin" <edwintorok@gmail.com>
To: David Allsopp <dra-news@metastack.com>,
	bluestorm <bluestorm.dylc@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Converting variants with only constant constructors to	integers
Date: Mon, 07 Jun 2010 22:46:16 +0300	[thread overview]
Message-ID: <4C0D4C88.4070102@gmail.com> (raw)
In-Reply-To: <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com>

On 06/07/2010 09:48 PM, David Allsopp wrote:
> Török 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 = A | B;;
> external int_of_foo : foo -> int = "%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.

Thanks, that "%identity" looks exactly what I wanted.
I don't need a foo_of_int right now.

On 06/07/2010 09:45 PM, bluestorm wrote:
> 2010/6/7 W Dan Meyer <wojciech.meyer@googlemail.com
> <mailto:wojciech.meyer@googlemail.com>>
>> I can imagine you could use an association list, function with pattern
>> matching, code generation tool (based on CamlP4), etc.
>
> I wrote a type-conv (
> http://www.ocaml.info/home/ocaml_sources.html#type-conv ) generator for
> just that a few years ago :
>    http://bluestorm.info/camlp4/ty_enum_to_int.ml.html
>
> Example use :
>
>   type test = | A | B | C | D of bool with to_int
>
>
> Generated functions :
>
>   let test_to_int = function | A -> 0 | B -> 1 | C -> 2 | D _ -> 3
>
>   let test_of_int = function
>   | 0 -> A
>   | 1 -> B
>   | 2 -> C
>   | 3 -> failwith "can't convert to a constructor with parameters: D"
>   | _ -> invalid_argument "test_of_int"
>

If I'll need a foo_of_int this sounds like a good solution, thanks.

Best regards,
--Edwin


  reply	other threads:[~2010-06-07 19:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-07 18:07 Török Edwin
2010-06-07 18:25 ` [Caml-list] " W Dan Meyer
2010-06-07 18:45   ` bluestorm
     [not found] ` <87sk4y7lc7.fsf@gmail.com>
     [not found]   ` <4C0D3B0F.4060502@gmail.com>
2010-06-07 18:32     ` Török Edwin
2010-06-07 18:50       ` W Dan Meyer
2010-06-07 18:48 ` David Allsopp
2010-06-07 19:46   ` Török Edwin [this message]
2010-06-07 19:56     ` bluestorm
2010-06-07 22:51       ` W Dan Meyer
2010-06-08  7:42         ` David Allsopp
2010-06-08  7:59           ` bluestorm
2010-06-08  9:14             ` David Allsopp
2010-06-08  9:36               ` Luc Maranget
2010-06-08  9:45                 ` David Allsopp
2010-06-08  9:51                   ` Luc Maranget
2010-06-08 10:21                     ` David Allsopp
2010-06-08 10:21                   ` Dario Teixeira
2010-06-08 11:28       ` Kaustuv Chaudhuri
2010-06-08 11:40         ` bluestorm
2010-06-08 14:37           ` Jacques Garrigue
2010-06-08 18:22             ` Kaustuv Chaudhuri
2010-06-09  1:34               ` Jacques Garrigue
2010-08-23 14:36               ` Damien Doligez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C0D4C88.4070102@gmail.com \
    --to=edwintorok@gmail.com \
    --cc=bluestorm.dylc@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=dra-news@metastack.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox