How can I get the types for the keyword arguments of a function? For example, with normal arguments I can use
f(x::Int) = x
methods(f)[1].sig
#Tuple{typeof(f), Int64}
I’m looking for something along the lines of
g(x::Int; y= 10 ) = x + y
magical_function(g)
# (y = Int64,)
I figured something out
using CodeTracking, ExprManipulation, ExprTools
using Base.Meta
function fn_expr(fn)
ms = methods(fn)
m = ms[1]
types = tuple(m.sig.types[2:end]...)
Meta.parse(code_string(fn, types))
end
function fn_kwargs(fn)
expr = fn_expr(fn)
def = splitdef(expr)
d = Dict()
m_expr = MExpr(:kw, Capture(:var), Capture(:expr))
for i in def[:kwargs]
m = match(m_expr, i)
d[m.var] = eval(m.expr)
end
d
end