(x,y) .+= z

question

#1

The following example does not work:

x = y = 0.
x, y .+= 2.

I’d expect this to add 2 to x and y, but instead it throws an error.

Is there a reason for this?


#2

.+= is equivalent to a broadcast! call, but you can’t broadcast! to a tuple because tuples are immutable.


#3

That makes sense, of course, but what makes you expect the behaviour to work is that you can assign to a tuple (x,y = 2,2) even though it is immutable. I guess this could be considered similar (irrespective of the technical argument)?


#4

Precisely, I was just about to say something like that.


#5

What do you mean “assign to a tuple”. You are just creating a tuple there with two expressions (that happen to also initialize two variables).


#6

Not sure I follow? Could you explain a bit more? I read the expression as (x, y) = (2,2) (that also parses). To my view, that’s a tuple on the LHS. Do you mean this parses to (x = 2, y = 2), i.e. a tuple of two expressions? (I mean I’m sure it does in the inside, but that’s not visible to the user).
This works as expected: x,y = (x, y) .+ 2 .


#7

Wow, I am sure that is what you wrote in your post… You didn’t edit it, hehe? Actually, I see now that assignments aren’t even allowed inside a tuple…


#8

No edits, promise :slight_smile: