Important: I am trying to figure this out to compare to a Python approach, which I mention in the second part of the question.
Suppose I have two functions, foo
and bar
, whose keyword argument I don’t know or care.
Suppose I want to write a function that calls these two using arbitrary keyword arguments.
For instance:
# Explicit kwargs
function solve_and_plot(problem; tol=1e-4, color=:red)
solve(problem, tol=tol)
plot(problem, color=color)
end
# More generic
function solve_and_plot(problem; kwargs...)
solve(problem; kwargs...)
plot(problem; kwargs...)
end
Is there a way to do it magically?
I normally solve this using Dicts for each function, what other approaches do you do?
The Python approach to do it magically is using decorators to change the AST.
Assuming there is no current way to do it implemented, I though I would try to do it to learn more about AST and Macros.
Using MacroTools, it is almost feasible at my current understanding, but I am missing how to find the (types and) default values of keyword arguments.
Answers to this question instead are also appreciated.
Thanks in advance.