Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can't you just have a NonZeroInt type?


Only if your language supports dependent types.


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: