Browse thread
Re: [Caml-list] C-like macros in OCaml
- David Allsopp
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | David Allsopp <dra-news@m...> |
| Subject: | Re: [Caml-list] C-like macros in OCaml |
On Jun 26, 2007, at 6:06 PM, Jonathan Bryant wrote:
> On Jun 26, 2007, at 6:00 PM, Raj B wrote:
> > Similarly, is there any way of getting a C-like enumeration? e.g.
> >
> > enum days {Mon = 1, Tue, Wed...}
>
> You can use a variant and a pair of functions:
>
> type weekday =
> | Monday
> | Tuesday
> ...
> | Sunday
You can also use a variant and Obj.magic which turns the two functions into:
let weekday_to_int (w : weekday) = Obj.magic w + 1
let int_to_weekday i =
if i >= 1 && i <= 7
then (Obj.magic (i - 1) : weekday)
else raise ...
But this is really dirty and a potential source of bugs... but the result is
closer to the "#define" way.
David