Reading the Real World OCaml book I came across the following type declaration (Chapter 6: Variants):
# type color =
| Basic of basic_color * weight (* basic colors, regular and bold *)
| RGB of int * int * int (* 6x6x6 color cube *)
| Gray of int (* 24 grayscale levels *)
;;
type color =
Basic of basic_color * weight
| RGB of int * int * int
| Gray of int
I thought the RGB and Gray variants could be constrained further. For example, each int in the RGB tuple should only be able to have the values 0-5.
In Erlang I'd do that like this:
-type rgbint() :: 0 | 1 | 2 | 3 | 4 | 5.
-type rgb() :: {rgb_int(), rgb_int(), rgb_int().
However, when I tried this in OCaml (in utop), it complained:
# type rgbint = 0 | 1 | 2 | 3 | 4 | 5 ;;
Error: Parse error: [type_kind] expected after "=" (in [opt_eq_pluseq_ctyp])
Questions:
- In OCaml, is it not allowed to use literals on the RHS of a type definition?
- How in OCaml would I do something like the Erlang rgbint() definition above?
With thanks and best wishes
Ivan