struct Foo<T>(T);
fn test<T, U>() {
type A = Foo<T>;
type B = Foo<U>;
let a = A::method();
let b = B::method();
// and so on...
}
This is not possible because of an error: can't use type parameters from outer function; try using a local type parameter instead.
There are probably good reasons for this error but I really think there should be a way to accomplish this kind of aliasing. Otherwise it makes some code noisy and hard to read:
let a = Foo::<T>::method();
let b = Foo::<U>::method();
...especially when you use Foo<T> and Foo<U> _a lot_.
There's a similar problem for impls. For example, I'd like this code to compile:
use std::marker::PhantomData;
struct Foo<Bar> {
foo: PhantomData<Bar>,
}
impl<Bar> Foo<Bar> {
type Qux = Vec<Bar>;
fn baz() -> Qux {
vec![]
}
}
But I get associated types are not allowed in inherent impls. I'm thinking we'd have to make a new keyword for type aliases to allow this.
Associated constants would be nice in both fn bodies and inherent impls too. I'd think structs could benefit from all three as well.
I'm thinking we'd have to make a new keyword for type aliases to allow this.
No, we just have to make associated types allowed in inherent impls. This is actually a part of the RFC which introduced the concept IIRC.
On the implementation side this has been hard because looking up associated types happens very early on, before a coherent list of impls is even built. We should be able to address this Soon (TM).
This is essentially ScopedTypeVariables afaik.
@eddyb are we there yet? (re. Soon (TM))
Most helpful comment
There's a similar problem for
impls. For example, I'd like this code to compile:But I get
associated types are not allowed in inherent impls. I'm thinking we'd have to make a new keyword for type aliases to allow this.