37 ms·
I frequently wish I had a natural number type. So many programs would benefit from the type system guaranteeing that numbers are never negative.
by bspammer 2y ago
I frequently wish I had a natural number type. So many programs would benefit from the type system guaranteeing that numbers are never negative.
- frogulis 2y agoEasy enough to write in some languages... as long as you only need to support addition and multiplication, or a very limited set of numbers.
- josephg 2y agoRust has this one - though the ergonomics are a little awkward. Its called NonZero: https://doc.rust-lang.org/std/num/type.NonZeroU32.html https://doc.rust-lang.org/std/num/type.NonZeroU32.html
- archargelod 2y agoNim has Natural and Positive (signed int) types that checked at compile and runtime. You can also define arbitrary range types for any ordinal type, for example enums: type Month = enum Jan, Feb, Mar, Apr, May, ... SpringMonth = range[Mar..May] var m: SpringMonth = Mar It will raise under/overflow exception if value falls out of range.