Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default type parameters are not in the language reference #24

Open
steveklabnik opened this issue Mar 23, 2017 · 4 comments
Open

Default type parameters are not in the language reference #24

steveklabnik opened this issue Mar 23, 2017 · 4 comments
Labels
A-types Area: Types New Content Missing features or aspects of language not currently documented.

Comments

@steveklabnik
Copy link
Member

Migrated from rust-lang/rust#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.

@Havvy Havvy added Enhancement Improvement in a non-prose section of the book New Content Missing features or aspects of language not currently documented. and removed Enhancement Improvement in a non-prose section of the book labels Sep 24, 2017
@ehuss ehuss added the A-types Area: Types label Mar 11, 2019
@mankinskin
Copy link

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.

@SilvanCodes
Copy link

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

@Havvy
Copy link
Contributor

Havvy commented Nov 9, 2020

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

@ehuss
Copy link
Contributor

ehuss commented Jan 13, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-types Area: Types New Content Missing features or aspects of language not currently documented.
Projects
None yet
Development

No branches or pull requests

5 participants