Question on Copying Variables

Do variables create shallow, or deep copies when assigning one to another? Moreover, what is the behavior with arrays when assigning an array to another array. Lastly, is there any way to modify this behavior? (Create shallow or deep copies with syntax)

Deep, stopping only at instances of classes, which are referenced.

In Verse, arrays are simple immutable values. Reading an array variable gives you an immutable array, and writing an array variable replaces the whole array with a new one.

In Java and C#, arrays work differently. In Java and C#, mutable arrays are objects that are passed around by reference, and comparing two arrays tells you whether they are they are the same mutable array. Whereas in Verse, comparing two arrays tells you whether they have the same length and have equal elements.

5 Likes

Are arrays and maps in verse possibly built using copy on write (COW :cow:) optimization under the hood?

So if a really heavy array is being copied, it is not truly copied, as they would be transparently baked by an object. If you then modify another instance, it will result in copy + modification operation, leaving any other instances untouched.

I understand what immutable means, but that does not affect how copies are produced.

Just curious.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.