Project

General

Profile

Actions

Feature #8190

closed

Truly parallel assignment

Added by Lavir_the_Whiolet (Lavir the Whiolet) almost 11 years ago. Updated almost 11 years ago.

Status:
Rejected
Target version:
-
[ruby-core:<unknown>]

Description

=begin
i = 0
s = [1,2,3]
i, s[i] =s[i], 7
p s
#=> [1, 7, 3]

s is expected to be [7, 2, 3].
=end

Updated by wardrop (Tom Wardrop) almost 11 years ago

=begin
Hmm, yeah that request seems reasonable. Right now it seems to be assigning left-to-right. So (({i})) is assigned (({s[i]})), making (({i})) equal to (({1})), only then is (({s[i]})) evaluated and assigned (({7})) as if it's a separate expression on a new line.

Could be a difficult change to make at least in terms of backwards compatibility, but the suggested behaviour is less surprising than the current behavior in my opinion.
=end

Updated by marcandre (Marc-Andre Lafortune) almost 11 years ago

  • Category set to core
  • Assignee set to matz (Yukihiro Matsumoto)

My first reflex would be to say "don't write code like this".

Do you have an actual use case?

FWIW, CoffeeScript handles this case the same way as MRI does.

Updated by nobu (Nobuyoshi Nakada) almost 11 years ago

The evaluation obeys the left-to-right order.
It doesn't seem like a bug to me.

Updated by Hanmac (Hans Mackowiak) almost 11 years ago

=begin

((%Lavir_the_Whiolet when you change this you will break code like%))

(({a,b = b,a}))

wich is a feature of ruby

Updated by Anonymous almost 11 years ago

I think it's good that Lavir is bringing this up, although I share MA's
feeling. I don't see how his proposal it would break "a, b = b, a". On the
contrary, "a, b = b, a" would only break if the assignment indeed worked
sequentially looks questioningly @ wardrop & nobu. I do not know whether
this slightly counterintuitive behavior is a problem of that requires
change in the code behavior, or just better documentation to remove the
feeling of surprise, but I do not regard design by jury the best approach
in this case.

Updated by matz (Yukihiro Matsumoto) almost 11 years ago

  • Category deleted (core)
  • Status changed from Open to Rejected

As Nobu stated the evaluation order goes from left to right.
And if you change this, you will introduce incompatibility.

Matz.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0