-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
…:…` in stdlib docs
(rust_highfive has picked a reviewer for you, use r? to override) |
This looks great! @bors r+ |
📌 Commit 698bbe5 has been approved by |
Also, is it just me, or does it seem like in the context of an |
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
}
} |
@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 |
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.). |
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.
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
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 ofSelf
, 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.