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

**URL:** <https://discourse.julialang.org/t/given-a-function-with-only-one-method-how-to-determine-the-number-of-arguments-that-it-accepts/88030>\
**Category:** General Usage\
**Tags:** argument, function\
**Created:** [September 30, 2022, 10:16am UTC](https://discourse.julialang.org/t/given-a-function-with-only-one-method-how-to-determine-the-number-of-arguments-that-it-accepts/88030 "2022-09-30T10:16:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 30, 2022, 10:16am UTC](https://discourse.julialang.org/t/given-a-function-with-only-one-method-how-to-determine-the-number-of-arguments-that-it-accepts/88030/1 "2022-09-30T10:16:08Z")

</div>

And would it be possible to determine this statically?

An example of what I want

```julia
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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 30, 2022, 10:57am UTC](https://discourse.julialang.org/t/given-a-function-with-only-one-method-how-to-determine-the-number-of-arguments-that-it-accepts/88030/2 "2022-09-30T10:57:26Z")

</div>

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
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.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 30, 2022, 10:59am UTC](https://discourse.julialang.org/t/given-a-function-with-only-one-method-how-to-determine-the-number-of-arguments-that-it-accepts/88030/3 "2022-09-30T10:59:55Z")

</div>

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.
