`outer` keyword to automatically `local` a variable?

rfourquet already demonstrated that an implicit local statement can shadow an already existing outer local variable, so best case is doing it only when there isn’t any in the outer local scopes.

When there are no outer locals to shadow, putting one right outside the for-loop makes sense. However, as StefanKarpinski said, variable isn’t guaranteed to be initialized because the for-loop isn’t guaranteed to run e.g. for outer i in iter where iter is empty. There’s no reasonable implicit default value to fix that. Relying on an iterable to be non-empty to initialize a variable is very likely a bug, and I prefer it to be caught by a syntax error at definition before an undefined error at some of the calls:

julia> function f(n)
         local i # implicit proposal
         for outer i = 1:n end
         return i
       end;

julia> f(1)
1

julia> f(-1)
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] f(n::Int64)
   @ Main ./REPL[4]:4
 [2] top-level scope
   @ REPL[5]:1

julia> function f(n)
         for outer i = 1:n end
         return i
       end;
ERROR: syntax: no outer local variable declaration exists for "for outer"
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

So, the consequences of the implicit local i proposal is actually substantial.

4 Likes