* [ruby-dev:51056] [Ruby master Bug#4443] odd evaluation order in a multiple assignment
[not found] <redmine.issue-4443.20110224232852.18@ruby-lang.org>
@ 2021-04-20 0:11 ` merch-redmine
2021-05-05 15:46 ` [ruby-dev:51059] " eregontp
1 sibling, 0 replies; 2+ messages in thread
From: merch-redmine @ 2021-04-20 0:11 UTC (permalink / raw)
To: ruby-dev
Issue #4443 has been updated by jeremyevans0 (Jeremy Evans).
I have submitted a pull request to fix multiple assignment evaluation order: https://github.com/ruby/ruby/pull/4390
----------------------------------------
Bug #4443: odd evaluation order in a multiple assignment
https://bugs.ruby-lang.org/issues/4443#change-91614
* Author: mame (Yusuke Endoh)
* Status: Assigned
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: -
----------------------------------------
遠藤です。core に投げてしまったようなので登録し直し。
Ruby は左から右に評価が進むと信じていたのですが、多重代入で裏切られました。
```ruby
def foo
p :foo
[]
end
def bar
p :bar
end
x, foo[0] = bar, 0
```
bar より foo が左にあるので、:foo 、:bar の順に出力されることを期待するのですが、なんと :bar 、:foo になります。
具体的に何が困るかというと、例えば
```ruby
obj, obj.foo = obj.foo, obj
```
には swap を期待するわけですが、そうなりません。こういうコードは実際に、木の回転などを実装するときにしばしば書きたくなります。この挙動に気がついたのも splay tree を実装していたときでした。こんなの:
```ruby
t.left, t.left.right, t = t.left.right, t, t.left
```
1.9 系列で修正すべきとまでは思いませんが、2.0 で直る可能性はあるでしょうか。
IRC で話したら「それで普通」みたいな反応もありましたが、
```
foo[0] = bar
```
はちゃんと :foo 、:bar の順に出ます。
--
Yusuke Endoh <mame@tsg.ne.jp>
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 2+ messages in thread
* [ruby-dev:51059] [Ruby master Bug#4443] odd evaluation order in a multiple assignment
[not found] <redmine.issue-4443.20110224232852.18@ruby-lang.org>
2021-04-20 0:11 ` [ruby-dev:51056] [Ruby master Bug#4443] odd evaluation order in a multiple assignment merch-redmine
@ 2021-05-05 15:46 ` eregontp
1 sibling, 0 replies; 2+ messages in thread
From: eregontp @ 2021-05-05 15:46 UTC (permalink / raw)
To: ruby-dev
Issue #4443 has been updated by Eregon (Benoit Daloze).
I wrote some concerns over this change in https://bugs.ruby-lang.org/issues/15928#note-10.
I think the previous semantics of multiple assignments are better for various reasons.
We could change single assignment order, always evaluate RHS first, like MRuby behaves, if consistency is wanted.
----------------------------------------
Bug #4443: odd evaluation order in a multiple assignment
https://bugs.ruby-lang.org/issues/4443#change-91847
* Author: mame (Yusuke Endoh)
* Status: Closed
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* ruby -v: -
----------------------------------------
遠藤です。core に投げてしまったようなので登録し直し。
Ruby は左から右に評価が進むと信じていたのですが、多重代入で裏切られました。
```ruby
def foo
p :foo
[]
end
def bar
p :bar
end
x, foo[0] = bar, 0
```
bar より foo が左にあるので、:foo 、:bar の順に出力されることを期待するのですが、なんと :bar 、:foo になります。
具体的に何が困るかというと、例えば
```ruby
obj, obj.foo = obj.foo, obj
```
には swap を期待するわけですが、そうなりません。こういうコードは実際に、木の回転などを実装するときにしばしば書きたくなります。この挙動に気がついたのも splay tree を実装していたときでした。こんなの:
```ruby
t.left, t.left.right, t = t.left.right, t, t.left
```
1.9 系列で修正すべきとまでは思いませんが、2.0 で直る可能性はあるでしょうか。
IRC で話したら「それで普通」みたいな反応もありましたが、
```
foo[0] = bar
```
はちゃんと :foo 、:bar の順に出ます。
--
Yusuke Endoh <mame@tsg.ne.jp>
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-05-05 15:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-4443.20110224232852.18@ruby-lang.org>
2021-04-20 0:11 ` [ruby-dev:51056] [Ruby master Bug#4443] odd evaluation order in a multiple assignment merch-redmine
2021-05-05 15:46 ` [ruby-dev:51059] " eregontp
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 NNTP newsgroup(s).