Version française
Home     About     Download     Resources     Contact us    
Browse thread
C-like macros in OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Bryant <jtbryant@v...>
Subject: Re: [Caml-list] C-like macros in OCaml

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

let weekday_to_int w = match w with
| Monday -> 1
| Tuesday -> 2
  ...
| Sunday -> 7

let int_to_weekday i = match i with
| 1 -> Monday
| 2 -> Tuesday
  ...
| 7 -> Sunday
| _ -> raise (Invalid_argument "int_to_weekday")

--Jonathan