For example
function f(x)
# do something
# then I assigned the outside variable name of 'x' to y
println(y)
end
f(1)
I will get
# something and
1
then,
a = 1
f(a)
I will get
# something and
"a"
Is it possible in julia? If not, how can I get my function operation log?
CodePudding user response:
Depending on what you need a simples macro that dumps function calls that still get executed could be:
macro logs(expr)
@info expr
expr
end
And this can be used as:
julia> a = π/2;
julia> @logs sin(a)
[ Info: sin(a)
1.0
CodePudding user response:
The most idiomatic way would be to slightly change your interface of f
and require a keyword argument:
julia> function f(;kwargs...)
for (k, v) in kwargs
println("$k = $v")
end
end
f (generic function with 1 method)
julia> f(a = 1)
a = 1
Alternatively (short of inspecting stack traces), you need something macro-based:
julia> struct Quot
expr
value
end
julia> macro quot(e)
return :($Quot($(QuoteNode(e)), $e))
end
@quot (macro with 1 method)
julia> function f2(x::Quot)
println(x)
end
f2 (generic function with 1 method)
julia> x = 2
2
julia> f2(@quot x)
Quot(:x, 2)