Feature #17768
closedProposal: Downward assignments
Description
Rightward assignments have been introduced since 3.0.
To be honest, I'm not a big fan of the syntax because it does not add a new dimension to Ruby.
Why don't we bring Ruby to the next dimension?
Proposal¶
I propose "downward assignments".
p(2 * 3 * 7) #=> 42
^^^^^var
p var #=> 6
This new syntax intercepts the intermediate value of a subexpression.
In the above example, the subexpression 2 * 3
is captured to var
.
You can capture multiple subexpressions in one line.
puts("Hello" + "World") #=> HelloWorld
^^^^^^^x ^^^^^^^y
p x #=> "Hello"
p y #=> "World"
This proposal solves some long-standing issues in Ruby.
Use case 1¶
Everyone has written the following code.
while (line = gets) != nil
p line
end
This code is not so bad, but there's something that has been on my mind: is it really good to put an assignment into a condition expression?
I'm afraid that it makes the loop condition unclear.
Unfortunately, it is difficult to keep the condition clear in Ruby.
If the assignment is removed from the condition, the code becomes even more unclear as follows.
while true
line = gets
break if line == nil
p line
end
By using my proposal, you can make the condition crystal-clear.
while gets != nil
^^^^line
p line
end
Use case 2¶
Consider that we want to get from an array the last element that meets a condition.
ary = [1, 2, 3, 4, 5]
ary.each {|elem| found = elem if elem.even? }
p found #=> 4
As you know, this code does not work.
We need to add found = nil
to declare the variable "found" in the outer scope.
But this is unarguably dirty.
My proposal allows to make the code very straightforward.
ary = [1, 2, 3, 4, 5]
ary.each {|elem| elem if elem.even? }
^^^^found
p found #=> 4
Use case 3¶
When writing a constructor, we need to write each field name whopping three times.
class C
def initialize(foo, bar)
@foo = foo
@bar = bar
end
end
My proposal mitigates the problem to two times.
class C
def initialize(foo, bar)
^^^@foo ^^^@bar
end
Patch¶
A proof-of-concept is attached.
$ cat test.rb
p(2 * 3 * 7)
^^^^^var
p var
while gets != nil
^^^^line
p line
end
ary = [1, 2, 3, 4, 5]
ary.each {|elem| elem if elem.even? }
^^^^found
p found #=> 4
$ echo -e "foo\nbar" | ./miniruby test.rb
42
6
"foo\n"
"bar\n"
4
Notes:
- The syntax allows only ASCII characters because "East Asian width" is a hell.
- My patch does not implement binding a method parameter (Use case 3).
- There are some known bugs. Look for them.
Compatibility¶
A line that suddenly starts with ^
is invalid currently.
This is why I chose "downward" since upward assignments are incompatible.
vvvv line
while gets
When the previous line continues, ^
is appropriately handled as an XOR binary operator.
x = 1
# The following is considered as: y = 2^x
y = 2\
^x
p x #=> 1
p y #=> 3
So, I think this proposal is 100% compatible.
Discussion¶
I'm unsure how should we handle this.
p(2 * 3 * 7)
^^^^^var
Files
Updated by ko1 (Koichi Sasada) over 3 years ago
super convenient!!1
Updated by znz (Kazuhiro NISHIYAMA) over 3 years ago
I think this syntax is not irb
friendly.
Updated by mrkn (Kenta Murata) over 3 years ago
Is this intentional?
$ cat test.rb
p(2 * 3 * 7)
^^^^^var1
^^^var2
p var1
p var2
$ ./miniruby test.rb
test.rb:3: syntax error, unexpected '^', expecting end-of-input
^^^var2
Updated by marcandre (Marc-Andre Lafortune) over 3 years ago
I'd like a shorthand for cases where we don't really need a variable...
while gets != nil
p ^^^^
end
(It took me a while to realize the date in Japan :-) )
Updated by mame (Yusuke Endoh) over 3 years ago
marcandre (Marc-Andre Lafortune) wrote in #note-4:
(It took me a while to realize the date in Japan :-) )
I waited until 00:00 UTC 🕛👍
Updated by shevegen (Robert A. Heiler) over 3 years ago
plot twist by mame similar to last year:
- It's actually not a joke. :)
plot twist by matz this year:
- He actually WILL approve joke suggestions this year.
Updated by Dan0042 (Daniel DeLorme) over 3 years ago
What an amazing, beautiful, and revolutionary idea!
But what about using an overbar (U+203E) instead of circumflex? It's not that hard to type and it brings extra clarity and visual pleasantness to the code, while eliminating any possibility of conflict with existing syntax.
p(2 * 3 * 7) #=> 42
‾‾‾‾‾var
p var #=> 6
puts("Hello" + "World") #=> HelloWorld
‾‾‾‾‾‾‾x ‾‾‾‾‾‾‾y
p x #=> "Hello"
p y #=> "World"
Updated by Nakilon (Victor Maslov) over 3 years ago
Bidirectional assignments should be implemented to make swapping values:
var1, var2 = var2, var1
much shorter:
var1
vv^^
var2
Updated by mame (Yusuke Endoh) over 3 years ago
- Status changed from Open to Rejected
I think April 1st ended in the world. I hope you enjoyed it!