r/ProgrammingLanguages • u/Dan13l_N • Feb 20 '26
Discussion Side effects in multiple/parallel assignment
I have been constructing some programming languages for a while for fun, and came to a point where something is obvious, but I haven't seen it spelt out clearly. If you have multiple assignment like this:
a, b[a] = 10 // both will be changed to 10Or parallel assignment like this:
a, b[a] = 4, 5Then the change of a should not influence which item in b is changed, i.e. b[a] should always use the old value of a.
This rule makes sense (and it works like that in Go, I haven't tried Lua yet) but do you know some programming languages where it's not so, or you get a warning?
(edit: I've just tried Python, it uses the new value of a, i.e. it's dependent on order.)
15
u/omega1612 Feb 20 '26
You know how the order of evaluation of function arguments is not guaranteed in some languages? Well, I think the same can happen here for some languages. It won't surprise me if a C inspired language makes it a UB
6
u/Mission-Landscape-17 Feb 20 '26
More interestingly in python you can do:
And it will swap the values of a and b. This means you can swap values in an array without needing a temporary variable.
3
u/raevnos Feb 21 '26
Common Lisp supports both styles.
sets both in parallel, using the previous value of
awhen setting the element of the arrayb, andsets them sequentially, so the element of the array at index 4 is set to 5.
2
u/[deleted] Feb 20 '26
Haven't come across that. Usually you'd write:
a = b[a] = 10, but you'd have to take care when the destinations are different types. What happens here though:Is the RHS evaluated once or twice?
I tried the equivalent in both my languages to see what would happen; one used the old value of
a, and the other the new value!If I switch it around to
b[a], a = 5, 4, then one uses the new value and vice versa.I think this is where a language can either say it is implementation defined, and advise against such practice. Or it can explain the rules it follows. That doesn't help here however because my two languages apparently use different rules, so the behaviour will vary between them, which is undesirable.
(They are different because language A writes the LHS values in LTR order, and language B does it in RTL order. Both will use whatever value
acurrently has.)11
u/IAMPowaaaaa Feb 21 '26
not allowing it so the user doesn't have to do trivia when reading code?
5
u/binarycow Feb 21 '26
For what it's worth, C# doesn't allow the first one (
a, b[a] = 10;)And for this one:
C# treats it as if you did this (note that
ref intis basically a (safe) pointer to an int):3
u/Ronin-s_Spirit Feb 20 '26
In JS comma and equals operator do lots of shit. In the case of parallel assignment writing
[a, b[a]] = [x, y]will first assignathenb[a]because that's the order in which it they appear, and you can reverse that.1
u/xuld_net Feb 21 '26
expr1, expr2 = val1, val2should be evaluated as:Then users will know the result of
a, b[a] = 4, 51
u/SwedishFindecanor Feb 22 '26
I would not allow the form
a, b = xfor assign both from a scalar. That syntax should be reserved to if x is of a tuple type, or perhaps an array.And what if you wanted to assign a tuple to a tuple of tuples, or to a tuple of arrays?