However, I want to use the type system to make sure I didn’t miss any of the colors. E.g. I add another color to my variant type, and forget to update my association list.
What would be nice is to use polymorphic variants and then detect that linfers as having a subset rather than the complete set.
Otherwise, you can repeat the values in a list and then make assertions against that list. This requires repeating yourself, but you are repeating yourself in a different away and mechanically comparing these repetitions, which is also what tests do for a program in general. And if the reference repetition is right next to the type, it’s not so annoying to maintain.
type basic_color =
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
let basic_colors = [Black; Red; Green; Yellow; Blue; Magenta; Cyan; White]
let l =
[
Black, "1";
Red, "2";
Green, "3";
Yellow, "4";
Blue, "5";
(*Magenta, "5";*)
Cyan, "6";
White, "7";
]
let () = assert (basic_colors = List.sort compare (List.map Pair.fst l))