How to access the variable accessible by a closure?

in the following a is accessible by gun():

function fun()
    a = 2.0
    function gun(b)
        return a + b
    end
end

g = fun()
g(3.0)

julia> g = fun()
(::var"#gun#10"{Float64}) (generic function with 1 method)

julia> g(3.0)
5.0

now the question is: if I have g given, how could I know “what” variables could it access? and “how” to access them?
Thanks

Maybe a better way but they are just stored as fields

julia> propertynames(g)
(:a,)

e.g.

julia> g.a
2.0

I’m not sure if doing this is public API if that matters to you

1 Like