Pre-RFC: Integer Templating

Let's start even smaller. Let's just allow usize for now. You can always cast to the other types. There are some flukes with the const evaluator regarding the different integer types.


Please make the constants uppercase, normal constants are already uppercase.


This RFC duplicates a lot of work of https://github.com/rust-lang/rfcs/pull/1062. Maybe this one should be postponed until the other RFC is merged, because then this RFC can simply be sugar for desugaring the following

fn adder<const N: usize>(m: usize) -> usize { N + m }
fn main() {
    assert_eq!(adder<42>(66), 108);
}

to that

trait UnnamedTrait {
    const N: usize,
}
fn adder<T: UnnamedTrait>(m: usize) -> usize {
    T::N + m
}

fn main() {
    struct UnnamedDummy42;
    impl UnnamedTrait {
        const N: usize = 42;
    }
    assert_eq!(adder<UnnamedDummy42>(66), 108);
}

3 Likes