3 ms·
> Rust has its uint-thats-not-zero Why do we need to single out a specific value. It would be way better if we also could use uint-without-5-and-42. What I w
by 1718627440 1y ago
> Rust has its uint-thats-not-zero
Why do we need to single out a specific value. It would be way better if we also could use uint-without-5-and-42. What I would wish for is type attributes that really belong to the type.
typedef unsigned int __attribute__ ((constraint (X != 5 && X != 42))) my_type;
- steveklabnik 1y agoThose are the unstable attributes that your sibling is talking about.
- 1718627440 1y agoYeah of course I can put what I want in my toy compiler. My statement was about standard C. I think that's what Contracts really are and hope this will be included in C.
- steveklabnik 1y agoOh sure, I wouldn’t call rustc a “toy compiler” but yeah, they’d be cool in C as well.
- int_19h 1y agoProper union types would get you there. If you have them, then each specific integer constant is basically its own type, and e.g. uint8 is just (0|1|2|...|255). So long as your type algebra has an operator that excludes one of the variants from the union to produce a new one, it's trivial to exclude whatever, and it's still easy for the compiler to reason about such types and to provide syntactic sugar for them like 0..255 etc.