When I type:
type even = int
I am creating a module-level alias for the int type, right? How can I guarantee evenness of values with this kinda wrapper type that should add more constraints on the value? Obviously, the value constructor should be responsible for such a check. Can I define a constructor function even () and guarantee that all values of type even are created using the even () constructor that performs a check for evenness? What is OCaml’s idiomatic approach to do stuff like that?
Note 1: Of course,
even: int -> even is not possible or requires exceptions that I’m trying to avoid at any cost, so that and the return value should be of type
even option, but it is not important detail here.
Note 2: I have no real problem that I’m solving. I just want to discuss the approach of how to work with such value restrictions in OCaml on type/meta level.