Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upSyntax `object.method` (without parentheses) is unused #1287
Comments
This comment has been minimized.
This comment has been minimized.
|
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
} |
This comment has been minimized.
This comment has been minimized.
|
@P1start |
nrc
added
the
T-lang
label
Aug 22, 2016
This comment has been minimized.
This comment has been minimized.
|
@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" |
This comment has been minimized.
This comment has been minimized.
What exactly - variant 1, variant 2, variant 3 of |
This comment has been minimized.
This comment has been minimized.
|
My vote is for variant 1, the others are confusing. Static method lookup should only occur with types and modules, not with bindings. |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I'd like to express my support for variant 1; seems like a nice bit of sugar :) |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
My vote is against them all :)
|
This comment has been minimized.
This comment has been minimized.
UFCS doesn't do adjustments. I agree though, |
This comment has been minimized.
This comment has been minimized.
How do you create a callable to an object's method, that takes the same arguments as invoking the method through the object ( |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
@nagisa that's similar to how calling those two functions look though, to call the |
This comment has been minimized.
This comment has been minimized.
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). |
This comment has been minimized.
This comment has been minimized.
|
Is solving all ambiguity problems by changing the operator out of the question?
|
This comment has been minimized.
This comment has been minimized.
Nothing is out of the question :) From the alternatives you provide I still like the Having said this |
petrochenkov commentedSep 20, 2015
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.methodto 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 rust-lang/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
methodreturns an object implementingFn?) had to be clarified.Another useful unused syntax, somewhat opposite to
object.methodis "field UFCS":Object::field.It would be really great to make
Object::fielda projection functionfn(Object) -> FieldType:However, ownership, i.e. the choice between
fn(Object) -> FieldType,fn(&Object) -> &FieldTypeandfn(&mut Object) -> &mut FieldTypeshould be taken into account somehow.