JuliaIO/Suppressor.jl

Variable Scope

Opened this issue · 2 comments

Can you explain better how to handle variable scope with @capture_out?

I tried a small example with the following script:

using Suppressor #import package
local y #define local variable
function f(x) #define function
    y = x^2
    println(y)
    return y
end

log = @capture_out y = f(2) #capture output

#print results
println("log: $log")
println("y = $y")

When I run the script from the REPL (i.e., include(script.jl)), I get the following:

log: 4

ERROR: LoadError: UndefVarError: y not defined

I figured out how to work around this: initialize y before calling @capture_out. If I assign it some value, then the output of f(2) does get stored. Maybe mention this in the readme?

I figured out how to work around this: initialize y before calling @capture_out. If I assign it some value, then the output of f(2) does get stored. Maybe mention this in the readme?

I had the same issue and resolved it following your instructions. I created a PR to update the README.md.