Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] enums in OCaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] enums in OCaml?
> So, what's so wrong with just using variables?
> 
> let Accel = 1;;
> let Stop = 0;;
> 
> seems pretty short to write and shortest to use...  Am I missing something
> ( elegant | obvious )?  Is that just ugly?

If you use sum types, the compiler will
* check that in any pattern matching, or collection of objects
  (e.g. list), you only use constructors of the same sum type
* check whether your pattern matches are exhaustive or not, and avoid
  adding a default "_ -> .." clause at the end

These two checks take something like half of the bugs in any
program. So yes, this is paramount to use sum types rather than named
constants. The only case you want to use named constants is when you
do not have an exhaustive list of cases available, and want to be able
to add cases afterwards, making the above checks impossible. Even in
this case you may want to go for hybrid solutions, including a sum
type for the cases you know, but this is a bit more high-level.

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr