I would like to propose the following change.
Summary
Permit type parameters for associated functions that does not receive self
(et. al.) to shadow type parameters specified in the impl to permit less verbose expressions when constructing instances of a generic type.
Example
Consider the following:
pub struct Foo<T, U>(Option<T>, Option<U>);
pub fn existing<T>() -> Foo<T, u32> {
Foo(None, None)
}
impl<T> Foo<T, u32> {
pub fn existing() -> Foo<T, u32> {
Foo(None, None)
}
// Would be supported through this proposal:
pub fn proposed<T>() -> Foo<T, u32> {
Foo(None, None)
}
}
fn main() {
let foo = existing::<u32>();
let foo = Foo::<u32, _>::existing();
// Would be supported through this proposal:
let foo = Foo::proposed::<u32>();
}
Motivation
This proposal permits writing constructors where you only need to specify relevant type parameters. The rest can be inferred.
(Thanks to @durka!) This appears to already be supported in the language through the following approach:
pub trait Same<T> {}
impl<T> Same<T> for T {}
impl<T> Foo<T, u32> {
pub fn proposed<U: Same<T>>() -> Foo<T, u32> {
Foo(None, None)
}
}
While the existing support could act as an argument against this proposal, I’d argue that it is a bit too esoteric. The fact that it works appears to be a proof of concept that it can be implemented by treating variable shadowing as syntactic sugar for the above.
Thanks!