Given a function with only one method, how to determine the number of arguments that it accepts?

And would it be possible to determine this statically?

An example of what I want

julia> f = x -> x
#5 (generic function with 1 method)

julia> applicable(f, 0)
true

julia> applicable(f, 0, 0)
false

From this, I may determine that the function has a method that accepts one argument, but doesn’t accept two (in my case, it’s really a choice between 1 vs many arguments). Would it be possible to determine this statically, without actually trying out combinations?

This is used internally in ApproxFun to determine whether to splat arguments while passing them to the provided function, and I’m trying to see if such an approach may be improved.

This seems pretty hacky. Is that really a good idea?

Anyway, I came up with this, from messing around with methods, not sure if it’s the best way (or if, indeed, there is any good way at all):

julia> only(methods((x,y,z)->x+y)).nargs - 1
3

julia> only(methods((x,y)->x+y)).nargs - 1
2

julia> only(methods(()->x+y)).nargs - 1
0

Using only to make sure there’s exactly one method.

I doubt that it’s a good idea, but I’m unsure how it’s used internally, so I’m not brave enough to make sweeping changes. Thanks for your suggestion, seems quite reliant on internals TBH, but I’m unsure if there’s any other way.