Suprising assignment behavior


#1

This is confusing (on 0.5.0): imagine calling a function that returns 2 values in a loop:

julia> f()=1,2
f (generic function with 1 method)

julia> a,b=f()
(1,2)

julia> a
1

julia> b
2
julia> u=v=zeros(2,1);i=1;

julia> u[i],v[i]=f()
(1,2)

I was surprised by this:

julia> u
2×1 Array{Float64,2}:
 2.0
 0.0

julia> v
2×1 Array{Float64,2}:
 2.0
 0.0

I suppose this is related to the immutability of tuples and not a bug, but it is really surprising.

Edit: Never mind. u and v are the same arrays of course. How does one delete posts?


#2

Your problem is here… u and v point to the same exact matrix, so when u[i],v[i]=f() is unpacked, it first sets the matrix item to 1, and then to 2. Since there’s only one matrix, they both show the 2.


#3

Thanks. Just figured it out myself but you beat me to it :slight_smile:


#4

When a solution has been found and posted (even if by yourself), please mark the relevant post ad the solution by pressing the corresponding button.


#5

and the corresponding button is? I see ‘bookmark’, ‘share’, ‘flag’ and ‘reply’ and ‘like’…


#6

There should be one with a check mark (tick mark).


#7

Presumably, only the OP can see that box.


#8

That would make some sense, but even on the threads i started i don’t see this? Might be a browser issue?


#9

Ah, that could be true.