-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
TRPL: UFCS #24664
Conversation
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); |
There was a problem hiding this comment.
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)
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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 cc @nrc |
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. |
Updated! |
} | ||
``` | ||
|
||
The only way to choose `Clone::clone()` over `Foo::clone()` is to use this |
There was a problem hiding this comment.
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.
c31f0cc
to
3ea68cb
Compare
@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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Hence/hence/
r=me with a minor nit, thanks @steveklabnik! |
AKA, two four-letter acronyms 😉
@bors: r=alexcrichton rollup |
📌 Commit b97b0e3 has been approved by |
AKA, two four-letter acronyms 😉 This feels a bit light, if there's other things I should add, let me know.
AKA, two four-letter acronyms 😉
This feels a bit light, if there's other things I should add, let me know.