Pre-Pre-RFC: Prefacing items in methods with `Self::` implements them as associated items

The idea is that you'd be allow to define associated items inside methods so you can use associated generics. Visibility would be restricted to inside the function.

Commented example:

struct Foo<T: Sized> {
    ...
}

impl<T> Foo<T> {
    // You can already do this
    const A: usize = core::mem::size_of::<T>();

    fn foo(&self) {
        // you still can't do this though.
        const B: usize = core::mem::size_of::<T>();

        // doing this would be equivalent to defining `B` outside this method,
        // but in the `impl` block except that it is only visible inside this method.
        const Self::B: usize = core::mem::size_of::<T>();
    }
}

What problem does it solve?

2 Likes

You can't use normally use generics specified by the impl block inside constants or other items.

You can have const { .... } blocks that refer to generics.

Note that the generic const items experiment also allows to work around this issue by allowing B to be generic and then instantiating it with T.

Not inside of constants so you'd have to reuse the block every time.

This is meant to be an mvp of those.