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

Unified function / method call syntax #289

Closed
rust-highfive opened this issue Sep 24, 2014 · 6 comments
Closed

Unified function / method call syntax #289

rust-highfive opened this issue Sep 24, 2014 · 6 comments

Comments

@rust-highfive
Copy link

Issue by visionmedia
Thursday Jun 06, 2013 at 14:12 GMT

For earlier discussion, see rust-lang/rust#6974

This issue was labelled with: B-RFC in the Rust repository


I'm not sure how this would play out with internals or if it wouldn't provide enough information to the compiler, but instead of something like:

impl Shape for Circle {
    fn draw(&self, s: Surface) { do_draw_circle(s, *self); }
}

If possible it would be great to just do something like:

fn draw(self: Circle, s: Surface) {
  // ...
}

Where circle.draw(surface) is just sugar for draw(circle, surface).

@m-bra
Copy link

m-bra commented Jan 18, 2015

One reason against it would be that when you unify the syntax, you open up the implementation interface of objects. You would not be able to tell wether a function/method is part of the implementation or not. If you see circle.draw(surface) now, you can be sure: this method is implemented for circle, it knows the circle well, i can trust it. It's part of circle. If you have function(circle, bla) you can clearly see that it is not part of the circle implementation. Unifying the syntax would mix functions and methods and may lead to instable APIs.

@target-san
Copy link

Sorry for interfering guys.
I personally see no problems with desugaring a.b() into b(a). By thinking 'I can't distinguish functions form methods' you can come to 'I can't distinguish methods of different traits, so let's force user to specify them'.
Though, this specific case can be implemented with some operator, like |>, thus having a|>b().
There's also other approach, C# extension methods. I don't like their idea of 'special' functions. What if I want chaining some ordinary functions?

To conclude, I'm completely for TJ's variant of UFCS. If not, then let's have at least some 'chaining' operator.

EDIT: @m-bra On your worries about sugared free function indistinguishable form method. Rust already has that trouble with extension traits. In fact, this approach (with 'special signature' functions) was used in C#, in a bit different manner.

@alexcrichton
Copy link
Member

This has basically all since been implemented, so closing.

@mikepurvis
Copy link

@tj @alexcrichton This issue is high in the Google results for queries related to Rust and unified call syntax. Could there be links please added to documentation explaining how it works, please?

@kennytm
Copy link
Member

kennytm commented Dec 11, 2020

See https://github.com/rust-lang/rfcs/blob/master/text/0132-ufcs.md and https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name for what is actually implemented.

Note that we only support calling Circle::draw(circle, surface) or Shape::draw(circle, surface) or <Circle as Shape>::draw(circle, surface), not draw(circle, surface).
Also you cannot define fn draw(self: Circle, surface: Surface) outside of an impl.

@Havvy
Copy link
Contributor

Havvy commented Dec 11, 2020

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

No branches or pull requests

7 participants