Hacker News new | ask | show | jobs
by whyonearth 2739 days ago
Only if your language supports dependent types.
2 comments

It's perfectly possible in languages with ordinary ADTs.

  data Nat = Z | S Nat
  data NonZeroNat = OnePlus Nat
  data NonZeroInt = Negative NonZeroNat | Positive NonZeroNat
C isn't one of those languages, though.
The analogous would be the Go-style represent-a-sum-badly-as-a-product,

  struct nonzero_t {
    int is_negative;
    unsigned int one_less_than_the_absolute_value;
  };
which, under interpretation, ranges from -(2^32) to -1 and +1 to +(2^32).
In Ada, you can define integer types that only accept a given range of values.