Skip to content

Add example using Self to reference #37386

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

Merged
merged 1 commit into from
Nov 6, 2016
Merged

Add example using Self to reference #37386

merged 1 commit into from
Nov 6, 2016

Conversation

johnthagen
Copy link
Contributor

When I first came across Self I had a hard time finding references to it in the docs (and it's also been asked about on StackOverflow.

I hope this example provides someone who comes across it for the first time a little more help. If there is a better way to show an example actually using Self, I'm happy to modify this. It was just the simplest place to start I could see.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Manishearth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@Manishearth
Copy link
Member

@bors r+ rollup

thanks!

@bors
Copy link
Collaborator

bors commented Oct 25, 2016

📌 Commit d211eeb has been approved by Manishearth

@Manishearth
Copy link
Member

@bors-servo r-

doctest fail

---- Type_system_8 stdout ----

    error[E0405]: trait `Printable` is not in scope

 --> <anon>:2:6

  |

2 | impl Printable for String {

  |      ^^^^^^^^^ `Printable` is not in scope

  |

  = help: no candidates by the name of `Printable` found in your project; maybe you misspelled the name or forgot to import an external crate?

Add the definition of trait Printable to that code block. Prefix it with # signs if you don't want it to show up in the doc output (but be used for doctest output)

@johnthagen
Copy link
Contributor Author

@Manishearth Sorry about that, should have checked the Travis build. Will check the Travis build after this latest fix.

Copy link
Member

@GuillaumeGomez GuillaumeGomez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good except the little typo.

@@ -3769,7 +3769,20 @@ impl Printable for String {

The notation `&self` is a shorthand for `self: &Self`. In this case,
in the impl, `Self` refers to the value of type `String` that is the
receiver for a call to the method `make_string`.
receiver for a call to the method `make_string`. Thus, it could also
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra whitespace before "Thus".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still pending.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuillaumeGomez Everything good now?

@GuillaumeGomez
Copy link
Member

Once done, can you squash your commits please? If you don't know how to do it, you can take a look here.

@johnthagen
Copy link
Contributor Author

@GuillaumeGomez Thanks for the advice. The link you shared seems a little out of date (no more git history), but this one had some good steps.

# }
#
impl Printable for String {
fn make_string(&self) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why returning Self whereas the trait method return String? To be more clear: even if you implement the trait on String, the method should still returns String and not Self for more clarity.

Copy link
Contributor Author

@johnthagen johnthagen Nov 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this may not be best example.

Edit: You're right, this is not a good example.

The purpose of the PR is that when I first ran in to Self in the wild (not self) I was confused how it worked since I hadn't seen something like that in another language. When I came to the docs, the examples used self but not Self directly. I wanted to get some example into the reference that actually used Self directly, so that someone else new would looked it up would immediately see it used in an example.

Is there a better way to show an example of actually using Self directly?

This is an example of what I ran into the wild and was looking for an example to help understand:

impl<I> From<AfterRenderArgs> for Event<I> {
    fn from(args: AfterRenderArgs) -> Self {
        Event::AfterRender(args)
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the From trait seems like an excellent excellent to demonstrate it. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let me know what you think of the latest update.

}
```

The notation `Self` in the impl refers to the implementing type, `String`. In another
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"implementing type: String."

@GuillaumeGomez
Copy link
Member

So except for the little typo, the example is now really great.

@johnthagen
Copy link
Contributor Author

Good catch, I think everything is fixed up now. Hey, and thanks for working through this with me. As someone fairly new to Rust, I appreciate it.

@GuillaumeGomez
Copy link
Member

I'm a reviewer, so that's part of my "job". Thanks a lot for your work!

@GuillaumeGomez
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Collaborator

bors commented Nov 6, 2016

📌 Commit 434c314 has been approved by GuillaumeGomez

@bors
Copy link
Collaborator

bors commented Nov 6, 2016

⌛ Testing commit 434c314 with merge 161f262...

bors added a commit that referenced this pull request Nov 6, 2016
…Gomez

Add example using Self to reference

When I first came across `Self` I had a hard time finding references to it in the docs (and it's also been asked about on [StackOverflow](http://stackoverflow.com/questions/32304595/whats-the-difference-between-self-and-self).

I hope this example provides someone who comes across it for the first time a little more help.  If there is a better way to show an example actually using `Self`, I'm happy to modify this.  It was just the simplest place to start I could see.
@bors bors merged commit 434c314 into rust-lang:master Nov 6, 2016
@johnthagen johnthagen deleted the Self-reference-example branch November 6, 2016 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants