News for hackers. Stuff that matters.
Re:What would it take to add refinement types to Rust? by bspammer on Tuesday December 24, 2024 @05:51PM (#42505521)
I frequently wish I had a natural number type. So many programs would benefit from the type system guaranteeing that numbers are never negative.
Re:What would it take to add refinement types to Rust? by josephg on Tuesday December 24, 2024 @07:02PM (#42505878)
Rust has this one - though the ergonomics are a little awkward. Its called NonZero:

https://doc.rust-lang.org/std/num/type.NonZeroU32.html

Re:What would it take to add refinement types to Rust? by frogulis on Tuesday December 24, 2024 @06:13PM (#42505649)
Easy enough to write in some languages... as long as you only need to support addition and multiplication, or a very limited set of numbers.
Re:What would it take to add refinement types to Rust? by archargelod on Tuesday December 24, 2024 @10:24PM (#42506625)
Nim 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.