Is it worth introducing `copy()` into learnxiny?

Sure–it’s an important part of Julia, but it’s probably worth being clear that this has nothing to do with arrays in particular; rather it is a property of how = works with everything in Julia. In fairness, though, new users will often first experience this in the context of an array just because they are so widely used.

Something like:

# Assignment with `=` attaches a new label to the same value without copying
a = [1, 2, 3]
b = a
# Now `b` and `a` point to the same value, so changing one affects the other:
a[3] = 5
b[3]  # => 5

# The `copy()` function can create a shallow copy of an array, dictionary,
# or other container
a = [1, 2, 3]
c = copy(a)
a[3] = 5
c[3] # => 3

might be a nice addition.

2 Likes