Version française
Home     About     Download     Resources     Contact us    
Browse thread
Ocaml compiler features
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Ocaml compiler features
Tom wrote:
> > you could do it like this instead
> >
> >  let carrot, apple, pizza, coke, sushi = 0, 1, 2, 3, 4
> >
> >  let in_supple = Array.create 5 0
> >
> >  in_supply.(coke) <- 20
> >
> > . What do you think?
> >
> > - Tom
> >
That gives me what I've asked for at the moment, but I really want
arrays indexed by atomic variant types in a type-safe way.  i.e. so I
could do:

type food = Carrot | Apple | Pizza | Coke | Sushi
let in_supply = Super_array.create food 0
in_supply.(Coke) <- 20
for f in food'range do
	printf "%s - %d" f'name in_supply.(f)
done

or even
Super_array.iteri (fun f sup -> printf "%s - %d" f'name %d)

This will require some magic in the compiler, and some trivially
auto-generated functions to get at the names of atomic variants.  Yes, I
know it could be left to the user to write the boring function:
let food_to_string = function Carrot -> "Carrot" | Apple -> "Apple" ...
but why should you do the work, if the computer's being paid to do it?

E.