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

Soup up 'method syntax' chapter of the Book #21108

Merged
merged 1 commit into from
Jan 25, 2015

Conversation

steveklabnik
Copy link
Member

Fixes #16969

@rust-highfive
Copy link
Collaborator

r? @pcwalton

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

## Chaining method calls

So, now we know how to call a method, such as `foo.bar()`. But what about our
original example, `foo.bar().baz()`? This is called 'method chaning', and we
Copy link

Choose a reason for hiding this comment

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

"method chaining"

@steveklabnik
Copy link
Member Author

Thanks @thommay

@thommay
Copy link

thommay commented Jan 13, 2015

Sorry that was a bit of a drive-by review :)

@steveklabnik
Copy link
Member Author

No sorries needed, I appreciate it very much!


We just say we're returning a `Circle`. With this, we can grow a new circle
that's twice as big as the old one. But what if we didn't want to make a new
Circle, but change the old one? There's a pattern where this is commonly used:
Copy link
Member

Choose a reason for hiding this comment

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

This sentence seems a little odd starting with "But" and then also having a conjunction "but"

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the second one should be 'and'

@steveklabnik steveklabnik force-pushed the gh16969 branch 2 times, most recently from d693938 to 82f43c7 Compare January 13, 2015 20:56
}

impl Circle {
fn x(&mut self, x: f64) -> &mut Circle {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be more convenient for the caller if the method takes and returns self by value?
eg: fn x(mut self, x: f64) -> Circle

Copy link
Member Author

Choose a reason for hiding this comment

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

Keep reading ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

If you're referring to the "Advanced Builder Pattern" section then it's missing.
If not then I don't know what I missed.

let circle: Circle = Circle::new().radius(5.0).coordinate(10.0); // Methods are by-value

seems a lot more usable to me than

let mut circle_builder = CircleBuilder::new();
circle_builder.radius(5.0).coordinate(10.0); // Methods are by-reference
let circle: Circle = circle_builder.finalise();

Edit: Or even the simplified case without an extra builder type:

let mut circle: Circle = Circle::new();
circle.radius(5.0).coordinate(10.0); // Methods are by-reference

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was, since the consuming form was what I was thinking of.

I think you're right, after seeing that code. Will change.

Copy link
Member

Choose a reason for hiding this comment

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

Note that builders by reference are currently preferred, but there are definitely tradeoffs both ways. This is meant to be a fairly trivial example, so the by-reference may not make much sense, but I've found that by-reference is almost always what you want for some serious configuration going on as you don't have to move it around all over the place.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wish that we had pubished the guidelines officially so I could link to them from here...

@steveklabnik
Copy link
Member Author

I did some significant editing here. What do you think @alexcrichton ?


let c = cb.coordinate(10.0)
.radius(5.0);
.finalize();
Copy link
Member

Choose a reason for hiding this comment

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

Does this compile? The finalize method consumes the builder but the previous methods are just returning references (and the builder doesn't have derive(Copy))

Copy link
Member Author

Choose a reason for hiding this comment

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

i ran rustdoc --test and it passed...

Copy link
Member

Choose a reason for hiding this comment

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

It looks like there's another error as well (the semicolon after the call to radius)

@steveklabnik
Copy link
Member Author

@alexcrichton addressed.

```
# struct Circle;
# impl Circle {
fn finalize(self) -> Circle {
Copy link
Member

Choose a reason for hiding this comment

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

I think this (and the below paragraph need to be udpated)

@alexcrichton
Copy link
Member

Just a small nit, otherwise r=me, thanks!

@steveklabnik
Copy link
Member Author

@bors: r=alexcrichton aca7939 rollup

flaper87 added a commit to flaper87/rust that referenced this pull request Jan 23, 2015
flaper87 added a commit to flaper87/rust that referenced this pull request Jan 24, 2015
bors added a commit that referenced this pull request Jan 25, 2015
@bors bors merged commit aca7939 into rust-lang:master Jan 25, 2015
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.

rust-guide::20 method syntax - Suggestion to explain method chaining and types of self
7 participants