caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Converting variants with only constant constructors to integers
@ 2010-06-07 18:07 Török Edwin
  2010-06-07 18:25 ` [Caml-list] " W Dan Meyer
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Török Edwin @ 2010-06-07 18:07 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2010-08-23 14:36 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-07 18:07 Converting variants with only constant constructors to integers 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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).