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

Replaced self-reflective explicit types with clearer Self or Self::… in stdlib docs #59275

Merged
merged 1 commit into from
Mar 19, 2019

Conversation

regexident
Copy link
Contributor

Many docs examples use explicit types instead of the semantically more clear Self/Self::… aliases.

By using the latter it's clear that the value's type depends on either Self, or an associated type of Self, instead of some constant type. It's also more consistent (and I'd argue correct), as the current docs aren't really consistent in this, as can be seen from the diff.

This is a best effort PR, as I was basically going through the docs manually, looking for offending examples. I'm sure I missed a few. Gotta start somewhere.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 18, 2019
@joshtriplett
Copy link
Member

This looks great!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 18, 2019

📌 Commit 698bbe5 has been approved by joshtriplett

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 18, 2019
@joshtriplett
Copy link
Member

Also, is it just me, or does it seem like in the context of an impl block with a type Output, you should be able to write -> Output instead of -> Self::Output?

@regexident
Copy link
Contributor Author

Would be nice, but how would one distinguish between associated and scoped types?

struct Foo;
struct Bar;
struct Foobar;

trait Trait {
    type Foo;

    fn foo() -> Self::Foo;
    fn also_foo() -> Foo;
}

// current syntax:
impl Trait for Foobar {
    type Foo = Bar;

    fn foo() -> Self::Foo {
       Bar // everything nice and clear
    }
    fn also_foo() -> Foo {
        Foo
    }
}

// discussed syntax:
impl Trait for Foobar {
    type Foo = Bar;

    fn foo() -> Foo {
        Bar // ☝🏻 Foo is actually Bar 🤷🏻‍♂️
    }
    fn also_foo() -> Foo {
        Foo
    }
}

@joshtriplett
Copy link
Member

@regexident Oh, I see. Given that associated types aren't in scope today, putting them in scope would break backward compatibility. :(

(If not for that, you could disambiguate by writing self::, as in "at module scope".)

@regexident
Copy link
Contributor Author

Swift does things the way you proposed and cannot express the latter without a sacrificial typealias on the outer type (within the outer scope).

Swift also implicitly turns generic arguments into public typealiases of the type. Nice in 90% of situations, super annoying in the remaining 10% (as your argument's name might collide with an associated type of a protocol, which in turn can collide themselves, etc. It's all but hygienic.).

Centril added a commit to Centril/rust that referenced this pull request Mar 19, 2019
Replaced self-reflective explicit types with clearer `Self` or `Self::…` in stdlib docs

Many docs examples use explicit types instead of the semantically more clear `Self`/`Self::…` aliases.

By using the latter it's clear that the value's type depends on either `Self`, or an associated type of `Self`, instead of some constant type. It's also more consistent (and I'd argue correct), as the current docs aren't really consistent in this, as can be seen from the diff.

This is a best effort PR, as I was basically going through the docs manually, looking for offending examples. I'm sure I missed a few. Gotta start somewhere.
bors added a commit that referenced this pull request Mar 19, 2019
Rollup of 11 pull requests

Successful merges:

 - #56348 (Add todo!() macro)
 - #57729 (extra testing of how NLL handles wildcard type `_`)
 - #57847 (dbg!() without parameters)
 - #58778 (Implement ExactSizeIterator for ToLowercase and ToUppercase)
 - #58812 (Clarify distinction between floor() and trunc())
 - #58939 (Fix a tiny error in documentation of std::pin.)
 - #59116 (Be more discerning on when to attempt suggesting a comma in a macro invocation)
 - #59252 (add self to mailmap)
 - #59275 (Replaced self-reflective explicit types with clearer `Self` or `Self::…` in stdlib docs)
 - #59280 (Stabilize refcell_map_split feature)
 - #59290 (Run branch cleanup after copy prop)

Failed merges:

r? @ghost
@bors bors merged commit 698bbe5 into rust-lang:master Mar 19, 2019
@regexident regexident deleted the docs-self branch March 19, 2019 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants