Skip to content

TRPL: UFCS #24664

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
Apr 23, 2015
Merged

TRPL: UFCS #24664

merged 1 commit into from
Apr 23, 2015

Conversation

steveklabnik
Copy link
Member

AKA, two four-letter acronyms 😉

This feels a bit light, if there's other things I should add, let me know.

@rust-highfive
Copy link
Contributor

r? @huonw

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

# }
# let b = Baz;
<Baz as Foo>::f(&b);
<Baz as Bar>::f(&b);
Copy link
Member

Choose a reason for hiding this comment

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

I believe another (perhaps more idiomatic) example which would work here is Foo::f(&b).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh. So what's a good example to show off this syntax?

Copy link
Member

Choose a reason for hiding this comment

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

Another example of using UFCS would be a conflicting trait and inherent method, but you want to call the trait method. For example if you had an inherent method called fn clone(&self) -> T then the only way to call the trait method would be through Clone::clone.

Although another example would be not having to import a trait to call the trait method as well. For example macros which call trait methods almost exclusively use UFCS syntax to ensure that the right method is called (e.g. they really want the trait method, not an accidental inherent method).

Copy link

Choose a reason for hiding this comment

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

Also, passing inherent/trait methods as closures by value (or generally treating them as values):

fn main() {
    println!("{:?}",
        vec![-5, 0, 20]
            .into_iter()
            .map(i64::abs)
            .collect::<Vec<_>>()
    );
}

I guess it's not UFCS as such, only the UF bit. :-) Which perhaps suggests a problem with our terminology since we don't have a good name for methods being functions with an implicit first argument. Or do we?

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, that's how I thought of it too: not exactly UFCS, but kinda

@alexcrichton
Copy link
Member

This seems pretty brief on the details here and it may be worth expanding a bit. For example all inherent methods are also accessible via UFCS and they're called like static methods except that they have an implicit first parameter. The full disambiguation syntax may also want to be explained as <Type as Trait>::method where as Trait is optional as well as the angle brackets.

cc @nrc

@steveklabnik
Copy link
Member Author

Yeah, I was reading the RFC, and it's very technical, and it's not a feature I myself use very often, so it's harder to find good examples. That stuff all sounds great.

@steveklabnik
Copy link
Member Author

Updated!

}
```

The only way to choose `Clone::clone()` over `Foo::clone()` is to use this
Copy link
Member

Choose a reason for hiding this comment

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

Technically Clone::clone would actually work here so the angle brackets aren't strictly necessary. I think it's still a fine example to have though.

@steveklabnik steveklabnik force-pushed the doc_ufcs branch 2 times, most recently from c31f0cc to 3ea68cb Compare April 22, 2015 22:04
@steveklabnik
Copy link
Member Author

@alexcrichton try this one

The `<>::` syntax is a means of providing a type hint. The type goes inside
the `<>`s. In this case, the type is `Type as Trait`, indicating that we want
`Trait`’s version of `method` to be called here. The `as Trait` part is
optional if it’s not ambiguous. Same with the angle brackets, too Hence the
Copy link
Member

Choose a reason for hiding this comment

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

s/Hence/hence/

@alexcrichton
Copy link
Member

r=me with a minor nit, thanks @steveklabnik!

AKA, two four-letter acronyms 😉
@steveklabnik
Copy link
Member Author

@bors: r=alexcrichton rollup

@bors
Copy link
Collaborator

bors commented Apr 22, 2015

📌 Commit b97b0e3 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Apr 23, 2015

⌛ Testing commit b97b0e3 with merge 1114fcd...

bors added a commit that referenced this pull request Apr 23, 2015
AKA, two four-letter acronyms 😉

This feels a bit light, if there's other things I should add, let me know.
@bors bors merged commit b97b0e3 into rust-lang:master Apr 23, 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.

5 participants