reference icon indicating copy to clipboard operation
reference copied to clipboard

Default type parameters are not in the language reference

Open steveklabnik opened this issue 7 years ago • 4 comments

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.

steveklabnik avatar Mar 23 '17 15:03 steveklabnik

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.

mankinskin avatar Nov 16 '19 01:11 mankinskin

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

SilvanCodes avatar Nov 08 '20 20:11 SilvanCodes

You should file an issue against rust-lang/rust about that. The reference just documents what's already there.

Havvy avatar Nov 09 '20 03:11 Havvy

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.

ehuss avatar Jan 13 '21 21:01 ehuss