Imagine the following scenario:
I have a function that generates an Expr
, which is in turn processed by eval
.
To make it easier, let’s create a small example:
function makeval()
ex =
Expr(:function,
Expr(:call, :addxy,
Expr(:(::), :x, :Int),
Expr(:(::), :y, :Int)),
Expr(:block,
Expr(:call, :+, :x, :y)))
eval(ex)
end
@info "Main names: " names(Main, all=false)
# will print:
#┌ Info: Main names:
#│ names(Main, all = false) =
#│ 4-element Vector{Symbol}:
#│ :Base
#│ :Core
#│ :Main
#└ :makeval
makeval()
@info "Main names: " names(Main, all=false)
# will print:
#┌ Info: Main names:
#│ names(Main, all = false) =
#│ 5-element Vector{Symbol}:
#│ :Base
#│ :Core
#│ :Main
#│ :addxy
#└ :makeval
@info "1 + 2 = " addxy(1, 2)
# will print:
#┌ Info: 1 + 2 =
#└ addxy(1, 2) = 3
Now, it is obvious that I could just do something with string(ex)
inside the function - and save it to some file if I wanted.
My question is this: is there an idiomatic way to save a snapshot of the current state (e.g., source code) of a module?
In my example, before executing the makeval
function, the addxy
function was not defined. However, after makeval
call, addxy
is my module scope and I can call it.
Don’t spend much time with the answer (OK, you can go into details if you really want) - but if there are a set of functions to do this, just point me in the right direction and I can figure it out and I can publish here how it went (for future reference).