How can I access the body of a function?
Context: I have functions inside modules which I execute with specific parameter values. I want to "keep a record" of these parameter values and corresponding functional forms. Below my attempt:
module MyModule
using Parameters # Parameters provides unpack() macro
using DataFrames # DataFrames used to store results in a DataFrame
struct ModelParameters
γ::Float64
U::Function
end
function ModelParameters(;
γ = 2.0,
U = c -> if γ == 1.0; log(c); else (c^(1-γ)-1)/(1-γ) end
)
ModelParameters(γ, U)
end
function show_constants(mp::ModelParameters)
@unpack γ = mp
d = DataFrame(
Name = ["γ"],
Description = ["parameter of U"],
Value = [γ]
)
return(d)
end
function show_functions(mp::ModelParameters)
@unpack U = mp
d = DataFrame(
Name = ["U"],
Description = ["function with parameter γ"],
Value = [U]
)
return d
end
export
ModelParameters
show_constants,
show_functions
end # end of MyModule
Keeping a record:
using Main.MyModule
mp = ModelParameters()
MyModule.show_constants(mp)
1×3 DataFrame
Row │ Name Description Value
│ String String Float64
─────┼─────────────────────────────────
1 │ γ parameter of U 2.0
MyModule.show_functions(mp)
1×3 DataFrame
Row │ Name Description Value
│ String String #2#4…
─────┼──────────────────────────────────────────
1 │ U function with parameter γ #2
This is quite useful to store scalars and arrays value, but not for functions. How could I replace #2 with something useful?
Examples of what would be useful:
c -> if γ == 1.0; log(c); else (c^(1-γ)-1)/(1-γ) end,
or
(c^(1-2.0)-1)/(1-2.0)
or (magically simplified):
1-c^(-1.0)
My question is somewhat related to Julia: show body of function (to find lost code).