Issue #9076 has been updated by asterite (Ary Borenszweig).


alexeymuranov (Alexey Muranov) wrote:
> In my opinion, this is a bad idea: there would be a dot `.`, an ampersand `&`, and an ampersand-dot `&.`, unrelated to either of the two.
> 
> What is wrong with `[1, 2, 3, 4].map{|x| x.to_s(2)}`?

The "x" variable is just noise. What you want to do in that line of code is "map every element with a call to to_s(2)". With an "x" what you read is "let x be each element of this array, then invoke to_s(2) on it...". The first sentence is shorter and clearer on the mind.

This is why people tend to do `&:to_s` instead of `{|x| x.to_s}`. The later just adds noise.

However, what does a Symbol has to do there? Why doesn't it work with a String, like `&"to_s"`? Well, because it's just a hack. It's an abuse of the language. And that abuse not only comes with a hard explanation (it calls the to_proc method on Symbol to turn it into a Proc) but also with a performance penalty.

By using `&.to_s` you get 1. no performance penalty, 2. a chainable syntax, 3. an easier way to explain it (it's just syntax sugar for a single argument block, where you invoke a method on it).

But maybe its too late to add it now to the language... nobody seems to like it or see its advantages.
----------------------------------------
Feature #9076: New one-argument block syntax: &.
https://bugs.ruby-lang.org/issues/9076#change-42750

Author: asterite (Ary Borenszweig)
Status: Feedback
Priority: Low
Assignee: 
Category: core
Target version: Next Major


Hello,

I'd like to introduce a new syntax for blocks that have one argument.

Currently you can do this:

[1, 2, 3].map &:to_s

With the proposed syntax this will be written as:

[1, 2, 3].map &.to_s

Instead of ":" we use a ".".

The idea is that this new syntax is just syntax sugar that is expanded by the parser to this:

[1, 2, 3].map { |arg| arg.to_s }

This new syntax allows passing arguments:

[1, 2, 3, 4].map &.to_s(2) #=> ["1", "10", "11", "100"]

It also allows chaining calls:

[1, 10, 100].map &.to_s.length #=> [1, 2, 3]

You can also use another block:

[[1, -2], [-3, -4]].map &.map &.abs #=> [[1, 2], [3, 4]]

Pros:
- Doesn't conflict with any existing syntax, because that now gives a syntax error, so it is available.
- Allows passing arguments and chaining calls
- It's *fast*: it's just syntax sugar. The "&:to_s" is slower because the to_proc method is invoked, you have a cache of procs, etc.
- It looks ok (in my opinion) and allows very nice functional code (like the last example).

Cons:
- Only supports one (implicit) argument. But this is the same limitation of "&:to_s". If you want more than one argument, use the traditional block syntax.
- It's a new syntax, so users need to learn it. But to defend this point, users right now need to understand the &:to_s syntax, which is hard to explain (this calls the "to_proc" method of Symbol, which creates a block... vs. "it's just syntax sugar for")

What do you think?

We are using this syntax in a new language we are doing, Crystal, which has a syntax very similar to Ruby, and so far we think it's nice, simple and powerful. You can read more about it here: http://crystal-lang.org/2013/09/15/to-proc.html


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