For loop globals in 0.7


#1

Can someone help me understand why the following loop has a deprecation warning in 0.7?

julia> t = 0.0;

julia> for j=1:10
           t += 0.1;
       end
┌ Warning: Deprecated syntax `implicit assignment to global variable `t``.
│ Use `global t` instead.
└ @ none:0

Other than just putting a global in to eliminate the warning, is there a “correct” way to accomplish this? I’d been going out of my way to avoid using globals, thus far.


#2

Putting the global on it is the correct way to do this now. For loops always introduce local scope and assignment in local scope always creates a new local variable now. The old, widely misunderstood behavior was that this would update a global of one was already defined and would otherwise introduce a new local. This lead to confusing/ed explanations about “soft” and “hard” scope. That distinction no longer exists: there is only one kind of scope. The flip side of this is that you need a global here. We might special case the REPL in the future to make this work the way you expect it to but what it would actually be doing would be transforming your code to this:

t = let t = t
     for j = 1:10
          t += 0.1
     end
end