I think I read somewhere in the docs that constants in Julia can be
modified, but only as long as the type is kept the same.

This is not ideal, but at least there’s a warning message about it. Except
that if the type is an array, and one modifies an entry in it, or modifies
a reference to this const, there is no warning…

const z = [0,0]
z = [1, 1]; #produces warning
z[1] = 2; #does not produce warning
y = z;
y[1] = 3; #does not produce warning

this has caused me some pain as having code like this can lead to confusing
behavior:


const z0 = [0,0]
function foo(x)
y = z0
if x != 0
  y[1] = x
end
return y
end
foo(0) #returns [0,0]
foo(1) #returns [1,0]
foo(0) #returns [1,0]

And of course at no point do I get a warning that z0 was changed…

I guess my question is how do I get around this? do I have to do a
copy(z0)? or is there a way to enforce immutability of constants?

Thanks,
-z
​

Reply via email to