reference
reference copied to clipboard
Default type parameters are not in the language reference
Migrated from https://github.com/rust-lang/rust/issues/38011
Default type parameters (e g struct Foo<A = u32>(A)
) is a language feature, but they are not mentioned in the language reference.
It would be nice if such a reference also mentioned the tricky semantics around how it interacts with inference, as mentioned here.
It would be even better if the semantics could be less tricky :) Where is the problem with always trying to infer unspecified types and falling back on defaults when they can't be inferred? This is the behaviour I would expect, at least.
I would like the first case to work:
struct Foo<T = u8>(T);
impl<T: Default> Default for Foo<T> {
fn default() -> Self {
Self(Default::default())
}
}
// This breaks with: error[E0282]: type annotations needed for `main::Foo<T>`
let a = Foo::default();
// This works.
let b: Foo = Foo::default();
See it live here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0577801bc59bb19cc6c61039863d130a
You should file an issue against rust-lang/rust about that. The reference just documents what's already there.
See also RFC 213.
And #636.
And https://internals.rust-lang.org/t/interaction-of-user-defined-and-integral-fallbacks-with-inference/2496.
Be sure to mention that Self
is not allowed.