4 ms·
>I wonder, why is it NonZeroU8, NonZeroU16, etc instead of something like NonZero<u8>, NonZero<u16>, etc? The RFC covers this. https://github.com/rust-lang/rfc
by Arnavion 8y ago
>I wonder, why is it NonZeroU8, NonZeroU16, etc instead of something like NonZero<u8>, NonZero<u16>, etc?
The RFC covers this. https://github.com/rust-lang/rfcs/blob/master/text/2307-concrete-nonzero-types.md https://github.com/rust-lang/rfcs/blob/master/text/2307-conc... Also see the RFC discussion at https://github.com/rust-lang/rfcs/pull/2307 https://github.com/rust-lang/rfcs/pull/2307
Basically the generic NonZero already existed, but it's hard to express safely when used with arbitrary user types.
>In theory, in the future, when Rust supports constants as template arguments, one might even add a const argument, the value that should be used as "null". E.g. if my u8 never reaches 255, I could use something like Never<u8, 255>.
This requires const generics, so it could happen when they're implemented. (This is also mentioned in the RFC discussion.)
- lmm 8y agoDoes "const generics" essentially mean singleton types?
- steveklabnik 8y agoThere are so many "slightly different but mostly the same" things in this space. In Rust, this is const generics: https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md https://github.com/rust-lang/rfcs/blob/master/text/2000-cons... struct RectangularArray<T, const WIDTH: usize, const HEIGHT: usize> { array: [[T; WIDTH]; HEIGHT], } const X: usize = 7; fn main() { let x: RectangularArray<i32, 2, 4>; let y: RectangularArray<i32, X, {2 * 2}>; } I haven't read enough about singleton types to know the exact details about their similarities, so I'm not sure.
- lordnaikon 8y agoIs this what is known as pi or dependent types?
- steveklabnik 8y agoI believe that this initial version is not quite fully pi types; we did have an RFC for those but it was deemed too large to start off with.
- eddyb 8y agoYes, but limited to being applied at compile-time, with constant arguments. Fully general dependent type would allow passing in runtime values, which is significantly harder to type-check and execute