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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -