Issue #12125 has been updated by Robert A. Heiler.


I think the &File->basename looks confusing since we also have
-> standalone now.

object->method reminds me a lot of php/perl and ruby uses the
prettier . instead, object.method.

I also think that :

.map{File.basename(_0)} # where `_0` is magic var

Is not good either. I like _ as a variable name a lot but on
its own, not with extra. :)

Yuki Kurihara's proposal is somewhat better as he does not 
have to use special constructs/tokens.

Dir["*/*.c"].map(&File.method.basename)

I believe that crystal allows some parameter for &; and I think
there have been earlier proposals in ruby too.

> But I think it makes sense: You have an object, and from that
> object you point at a method to get it as Method.

I do not think that this argument is a good one because the ->
is used in a dissimilar way, akin to Proc.new / lambda, 
whereas your syntax proposal would be more similar to php
and perl syntax style, which I think will be confusing in
addition to -> already having another method. So this is 
not good in my opinion.

----------------------------------------
Feature #12125: Proposal: Shorthand operator for Object#method
https://bugs.ruby-lang.org/issues/12125#change-57216

* Author: Stefan Merettig
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Hello,

The `&` operator lets one pass a `#call`-able object as block.

Really useful feature, but at the moment, if you want to pass a `Method` this way the syntax is not really concise:

`Dir["*/*.c"].map(&File.method(:basename))`

More often than not, at least I end up writing this instead `.map{|a| File.basename a}` which isn't that great either.

Thus, I want to propose adding a short-hand operator to the ruby language, which simply calls `#method` on an Object.

It could look like this: `an_object->the_method` which is 100% equivalent to doing `an_object.method(:the_method)`

I'm reusing the `->` operator which is already used for the stabby lambda. But I think it makes sense: You have an object,
and from that object you point at a method to get it as `Method`.

With this, the example from above becomes: `Dir["*/*.c"].map(&File->basename)`

I attached a proof of concept patch. When you apply this to trunk, you can try the example above yourself.
Do note however that this PoC also breaks stabby lambda for the moment. I'll work on fixing that the following
days.

Thank you for reading,
Stefan.

---Files--------------------------------
method_shorthand.diff (740 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>