Issue #12125 has been updated by Stefan Merettig.


Yukihiro Matsumoto wrote:
> but I don't think `->` is a good idea.

Other options I can think of are:

* Using `.>`: `the_object.>a_method`  While its look is nearer to a normal method call (Which I think is a plus), I fear that the period would be hard to see in some fonts. Another plus is that this would be a entirely new operator, so no (unintentional?) breaking changes in the parser.
* Using `<..>`: `the_object<a_method>`  Inspired by the look of generics/templates in other programming languages. Should not clash with existing code and parsers and is well-readable in fonts I guess. Maybe it doesn't read as well anymore though, and the intention of `<..>` may not be that clear at first to someone who doesn't know the syntax (yet). No idea if that is a concern.
* Using `&>`: `the_object&>a_method`  Also readable in any font I can think of. It's a spin on `&.`, reading like "and this"
* Using `|>`: `the_object|>a_method`  I think the *Elixir* language has this operator too (albeit with other semantics?). Its read like "and pipe it through this", so maybe it reads a bit like a shell script too?

Regards,
Stefan

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

* 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>