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: Julien Signoles <julien.signoles@g...>
Subject: Re: [Caml-list] Create a constraint between variant type and data list
Hello Sylvain,

2010/9/3 Sylvain Le Gall <sylvain@le-gall.net>

> I would like to define:
>
> type license = GPL | LGPL
>
> and
>
> let data = [ GPL, "GNU Public license";
>             LGPL, "GNU Lesser General Public license" ]
>
>
> I would like to enforce that all variants of license are in the
> association list.
>

As said by other guys, you can't enforce statically such an invariant
without a code generator.
In pure ocaml, the below solution use a "social check" : it works only if
the developer who adds a new constructor is not too bad and takes care of
context when solving an error ;-).

type license = GPL | LGPL

let data =
  [ GPL, "GNU Public license";
     LGPL, "GNU Lesser General Public license" ]

let () = match GPL with
  | GPL | LGPL ->
     assert
       (let s = "Do not forget to add new constructors to the data list
above." in
        Format.ifprintf Format.std_formatter "%s" s;
        true)

Hope this helps,
Julien