Version française
Home     About     Download     Resources     Contact us    
Browse thread
Create a constraint between variant type and data list
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: bluestorm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] Create a constraint between variant type and data list
Hi,

I do not have a direct solution to your problem.

If you want to associate a data with each case, you can use a pattern
matching, wich will do the exhaustiveness check :

let to_string : license -> _ = function
| `GPL -> "GPL"
| `LGPL -> "LGPL"

You will be warned if you add (or remove) some licenses and forget to
change the function.


You may, however, want to have a list of all licenses values, instead
of a case-handling on each value. I don't know how you could have the
compiler check completeness for you, but if you really want that
check, there is an extra-linguistic method : declare your value
without giving it a type annotation, so that the compiler will infer
the covered case, then use a script outside the program, calling
"ocamlc -i" to check the inferred signature, and comparing it to you
datatype declaration.

Finally, I have a third solution based on code generation : given my
first solution (turning the association list into a function), what
you need is only a list of all the constructors (and you can build
your assoc list with List.map (fun x -> x, assoc_function x)). This
can easily be generated from the datatype declaration using direct
camlp4, or Markus Mottl's type-conv (
http://www.ocaml.info/home/ocaml_sources.html#toc11 ).