>
> Even stranger, the values of x are not modified by the following function
> function f3(x::Array)
>  w = x + 0.
>  for i in 1:length(w)
>    w[i] = w[i] + 10.0
>  end
>  return w
> end
>

You think it is strange that changing w does not change x? Most seasoned 
programmers would be *shocked* if changes to w affected x in this code. The 
behavior you are seeing in Julia is very standard behavior; virtually any 
language will behave the same way (any language that supports arraywise 
operations, anyway, which is a small fraction of all programming 
languages). All seasoned programmers see that there is a big difference 
between "x" and "x + 0".

Would you agree that "w = x + 1" should produce a new array? Then w and x 
should be independent, right? If you run "w = x + 1", you wouldn't expect x 
to change right? And then if you change w, you still wouldn't expect x to 
change, right? Now imagine that the code is "w = x + i". If i is 0, it may 
seem appropriate to "optimize" this code into just "w = x". 

So imagine we do this "optimization". And imagine a program in which i > 0, 
99.9% of the time. In that case, w and x are independent 99.9% of the time 
and changes to w do not affect x. Knowing this, the author may *believe*that w 
and x are completely independent. But then one day, someone runs the 
program with i=0, so w and x are now the same array. Since the author was 
expecting w and x to be independent, he uses them as if they were 
independent, but when i=0, changing one array also changes the other, so 
the program will malfunction.

To avoid surprises like that, it makes sense to treat x + 0 the same as x + 
i for any i != 0.

Reply via email to