metaprogramming - Check if a function has keywords arguments in Julia -
is there way check if function has keywords arguments in julia? looking has_kwargs(fun::function)
return true if fun has method keyword arguments.
the high level idea build function:
function master_fun(foo::any, fun::function, ar::tuple, kw::tuple) if has_kwargs(fun) fun(ar... ; kw...) else fun(ar...) end end
basically, @michael k. borregaard's suggestion use try-catch correct , officially works.
looking unofficial implementation details, came followng:
haskw(f,tup) = isdefined(typeof(f).name.mt,:kwsorter) && length(methods(typeof(f).name.mt.kwsorter,(vector{any},typeof(f),tup...)))>0
this function first looks if there keyword processing on method of generic function, , if so, looks @ specific tuple of types.
for example:
julia> f(x::int) = 1 f (generic function 1 method) julia> f(x::string ; y="value") = 2 f (generic function 2 methods) julia> haskw(f,(int,)) false julia> haskw(f,(string,)) true
this should tested specific application, doesn't work when non-leaf types involved. michael commented, in question's context statement be:
if haskw(fun, typeof.(ar)) ...
Comments
Post a Comment