ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nakilon@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103152] [Ruby master Feature#17768] Proposal: Downward assignments
Date: Thu, 01 Apr 2021 14:21:34 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91225.20210401142133.18@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17768.20210401001031.18@ruby-lang.org

Issue #17768 has been updated by Nakilon (Victor Maslov).


Bidirectional assignments should be implemented to make swapping values:

```
var1, var2 = var2, var1
```

much shorter:

```
var1
vv^^
var2
```

----------------------------------------
Feature #17768: Proposal: Downward assignments
https://bugs.ruby-lang.org/issues/17768#change-91225

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
----------------------------------------
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"](http://www.unicode.org/reports/tr11/) 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--------------------------------
2021-aprilfool.patch (9.07 KB)


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

  parent reply	other threads:[~2021-04-01 14:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-01  0:10 [ruby-core:103135] [Ruby master Feature#17768] Proposal: Downward assignments mame
2021-04-01  0:21 ` [ruby-core:103136] " ko1
2021-04-01  0:53 ` [ruby-core:103137] " zn
2021-04-01  1:11 ` [ruby-core:103138] " muraken
2021-04-01  1:13 ` [ruby-core:103139] " marcandre-ruby-core
2021-04-01  1:23 ` [ruby-core:103141] " mame
2021-04-01 12:55 ` [ruby-core:103149] " shevegen
2021-04-01 13:27 ` [ruby-core:103150] " daniel
2021-04-01 14:21 ` nakilon [this message]
2021-04-02 12:13 ` [ruby-core:103181] " mame

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-91225.20210401142133.18@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).