2010/6/7 W Dan Meyer <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"