-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Syntax object.method
(without parentheses) is unused
#1287
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
Comments
Unfortunately, this would probably be backwards incompatible, as fields can share names with methods: struct Foo {
frob: &'static str,
}
impl Foo {
fn frob(&self) -> &'static str { "bar" }
}
fn main() {
let foo = Foo { frob: "foo" };
println!("{}", foo.frob); // foo
println!("{}", foo.frob()); // bar
} |
@P1start |
@petrochenkov any plans to pursue this? I've wanted this way too often and might fit in the 2017 roadmap of improving ergonomics. @oli-obk mentioned in IRC: "just prefer object.field over object.method if both field and method have the same name" |
What exactly - variant 1, variant 2, variant 3 of |
My vote is for variant 1, the others are confusing. Static method lookup should only occur with types and modules, not with bindings. |
@petrochenkov variant 1. Simple things should be simple. One can just pass a function as an argument, the same should be easily possible for object methods. Having to create a closure is an inconvenience when the compiler can do it for you. No need to think about |
I'd like to express my support for variant 1; seems like a nice bit of sugar :) |
So I wrote a [Pre-RFC] Unify bindings to callables in internals. In a nutshell it says that a binding to a callable always return a closure, and when the callable is of the form The main difference with variant 1, is that it also adds the possibility of binding associated functions using (note: since closures without environment work as function pointers, everything should just work) EDIT: i've removed the associated function syntax from the RFC. |
My vote is against them all :)
|
UFCS doesn't do adjustments. I agree though, |
How do you create a callable to an object's method, that takes the same arguments as invoking the method through the object ( |
@gnzlbg I suppose our expectations for what this ought to behave like differ. I feel like Option 1 is basically a proposal to add a different context-sensitive (something that would “activate” if there was |
@nagisa that's similar to how calling those two functions look though, to call the |
Why is @nagisa is the point that you are trying to make that
Kind of. It would return a closure, that would statically dispatch the method on a value of the object or a reference to it. No dynamic dispatch involved (because each closure is its own unique type and can perform static dispatch, as opposed to function pointers which type-erase the actual function involved), and no function pointer involved since only environment-less closures can decay to function pointers but these closures will always have an environment ( references can be involved, and technically "references are pointers", but this is the case with any method call in every situation). |
Is solving all ambiguity problems by changing the operator out of the question?
|
Nothing is out of the question :) From the alternatives you provide I still like the Having said this |
It's a valuable piece of syntax and it would be a shame not to put it into action some day.
Any plans/ideas how to do it better?
Several variants I can think about:
A method with bound self argument
is equvalent to
Swift goes this route. C# also allows converting
object.method
to a delegate, although explicitly.This is probably the most intuitive variant, but, frankly, I think it's pretty useless in practice (or at least rarely useful).
A variant of UFCS
is equvalent to
or
I'm not sure how useful is this. UFCS with adjustments (ref, deref, unsize etc.) would be pretty useful, it would allow libraries to evolve more freely (see Vec: looks like is_empty and len are not needed rust#26980 for example of what libraries can't do now if they want to keep backward compatibility), but it doesn't strictly need a value (
object
), only a type (Object
). I don't recall any language doing this.Parentheses elision
is equvalent to
This is what D does. Parentheses elision greatly reduces symbolic noise in typical code, but can
probably be confusing sometimes (I don't use D and don't know how confusing it is in practice) and
some corner cases (what happens if
method
returns an object implementingFn
?) had to be clarified.Another useful unused syntax, somewhat opposite to
object.method
is "field UFCS":Object::field
.It would be really great to make
Object::field
a projection functionfn(Object) -> FieldType
:However, ownership, i.e. the choice between
fn(Object) -> FieldType
,fn(&Object) -> &FieldType
andfn(&mut Object) -> &mut FieldType
should be taken into account somehow.The text was updated successfully, but these errors were encountered: