Arrays by Reference (?) The put method

I can’t seem to find documentation for this. Are Arrays handled by reference and if so how can I make them handle by value? I think the example below is self explanatory, I want x and y to be two unique arrays, as it stands that’s not seem to be the case.

x =[1,2,3,4,5]; y = x; y = y.put(0, 10); x.postln; y.postln;

As with most dynamic languages, variables are references to objects. If you want to make a copy, you have to do that explicitly:

a = [1, 2, 3];
b = a;
a == b; // equivalence: true -> same content
a === b; // identity: true (!) -> refer to the same object
c = a.copy;
c == b; // equivalence: true -> same content
c === b; // identity: false (!) -> refer to different objects
2 Likes

Exactly what I was looking for thank you!

I’m pretty certain that I should be able to find answers to this kind of questions in the documentation/forums, but unfortunately I can’t. I don’t know if it’s my search skills or the documentation’s fault.

Again, thank you for clearing this up!

Not necessarily. There are some questions where the search terms are too broad to be useful. “Reference” and “value” kinda fall into that category :wink: so don’t worry about it.

Documentation could perhaps be improved – I wouldn’t mind to write it up when I have time (which may not be immediate). Where would you have looked for this topic first?

hjh

I just tried searching “variables” in the help and an article on scoping and one on assignment come up - neither stresses the reference aspect. There are elsewhere a nice article on the difference between == and === but I can’t locate it atm. this is a detail that could maybe be shoehorned into some of the getting started tutorials, I definitely didn’t understand it clearly for a year or so!

This is one of those topics that programmers just take for granted. It would be good to explain this somewhere in the docs, maybe in the “Getting Started” guide.

I think what’s important for the documentation is to describe the way that one can manipulate data between variables. In my case I did understand that variables handle data by reference but I couldn’t for the life of me find the .copy method.

I think an overview tutorial on variables describing different SC variables, scope and data sharing/reference/manipulation would be useful.

I searched “variables by reference”, “variables by value” in google and in SC documentation.

This is a basic feature of programming languages such as javascript. I was ignoring this was also available in SC ! So doc is definitely missing around that.

I would link to a separate document from Getting Started.

My preferred mental image for this is: You have a bunch of objects on a big table. A variable (or reference from a collection) is just an arrow pointing to one of the objects.

It might be tempting to think of variables as boxes, each containing one object, but that visual analogy breaks down for a = something; b = a. You don’t have two boxes and two "something"s. There’s only one “something,” with two arrows pointing to it.

It’s pretty easy from there to make graphics illustrating the different cases, e.g.:

sc-multi-ref-array

hjh

This is a basic feature of programming languages such as javascript

In Python it would be == (equality) VS is (identity)

On the other hand, Lua only has == which compares by identity. This even works for string comparison because Lua strings are always interned. (It is possible, however, to override the behavior of the == with metamethods.)

So the behavior of == is not immediately obvious, even if you have previous experience with other programming languages.

Maybe referring to Python and JS can be helpful for those who already have a background in one of those languages.