Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in assignment: Numbers vs. Arrays #7861

Closed
juliohm opened this issue Aug 6, 2014 · 9 comments
Closed

Inconsistency in assignment: Numbers vs. Arrays #7861

juliohm opened this issue Aug 6, 2014 · 9 comments
Labels
won't change Indicates that work won't continue on an issue or pull request

Comments

@juliohm
Copy link
Contributor

juliohm commented Aug 6, 2014

I don't know if this is the intended behavior, could you please clarify?

a = b = 1
a = 0
# b is still 1
A = B = eye(3)
A[1,1] = 0
# B has also changed!

In GNU Octave, A and B point to the same address location as long as no write operation is performed. The expression A[1,1] = 0 tells to the language that it is time to copy the underlying data and make A and B two distinct objects.

@ivarne
Copy link
Member

ivarne commented Aug 6, 2014

This is definitely intended. You will have to take a copy (A = copy(B)), to actually get a different array.

@quinnj
Copy link
Member

quinnj commented Aug 6, 2014

This is entirely on purpose. A and B refer to the same Array, so changing one will change the other. There's this section in the FAQ that discusses a point related to this, but honestly, I think the chapter on variables in the manual would be an excellent place to have an explanation on this. Isn't there somewhere else this is mentioned in the manual though? Maybe I'm just thinking of the few times this has come up in the julia-users forum.

@juliohm
Copy link
Contributor Author

juliohm commented Aug 6, 2014

Is it not possible to mimic GNU Octave COW?

@StefanKarpinski
Copy link
Member

Is it not possible to mimic GNU Octave COW?

Absolutely – just copy your array before modifying it ;-)

@juliohm
Copy link
Contributor Author

juliohm commented Aug 6, 2014

Copying manually isn't the smartest solution.

@juliohm juliohm closed this as completed Aug 6, 2014
@quinnj
Copy link
Member

quinnj commented Aug 6, 2014

I would argue that's it's the most explicit and forces the programmer to think about if she really wants to assign the same reference object to two variables or if she wants two separate reference objects. No magic behind the back.

@JeffBezanson
Copy link
Member

Currently our arrays are true mutable arrays. We might have immutable arrays as well in the future.

@jiahao
Copy link
Member

jiahao commented Aug 6, 2014

See #5556 for a discussion on copy-on-write semantics.

@timholy
Copy link
Member

timholy commented Aug 6, 2014

You can also search old julia-dev discussions about copy-on-write. Bottom line is that many of us would view it as harmful. How do you implement push! if arrays are usually copy-on-write?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

7 participants