Redefining print method changes variable scoping in loop

Tetrakai
Sun, 24 Dec 2023 16:57:52 +0000
Internals & Design

Why does the first loop work but the second does not?

The MWE:
Summary (click for more details)
I get:

julia> using Printf

julia> for n in 1:10
               if n == 1
                       flag     = true
                       idx_last = 1
               else
                       flag     = false
                       idx_last = ifelse(flag, n, idx_last)
               end

           # Print Progress
               Base.show(io::IO, f::Float64) = @printf(io, "%.2f", f)
       end

julia> for n in 1:10
               if n == 1
                       flag     = true
                       idx_last = 1
               else
                       flag     = false
                       idx_last = ifelse(flag, n, idx_last)
               end

           # Print Progress
               # Base.show(io::IO, f::Float64) = @printf(io, "%.2f", f)
       end
ERROR: UndefVarError: `idx_last` not defined
Stacktrace:
 [1] top-level scope
   @ ./REPL[3]:7

10 posts - 3 participants