# Finding the number of arguments of an anonymous function

**URL:** https://discourse.julialang.org/t/finding-the-number-of-arguments-of-an-anonymous-function/24394
**Category:** General Usage
**Created:** [May 20, 2019, 9:35am UTC](https://discourse.julialang.org/t/finding-the-number-of-arguments-of-an-anonymous-function/24394 "2019-05-20T09:35:30Z")
**Posts on this page:** 1
**Showing post:** 29

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 20, 2019, 3:28pm UTC](https://discourse.julialang.org/t/finding-the-number-of-arguments-of-an-anonymous-function/24394/29 "2019-05-20T15:28:49Z")

</div>

FWIW, `applicable` seems to fare fairly well and should be safe:

```julia
julia> a(f) = applicable(f, 1) ? f(1) : f(1,2);

julia> t(f) = try; f(1); catch; f(1,2); end;

julia> m(f) = first(methods(h)).nargs == 1 ? f(1) : f(1,2);

julia> @btime a($h)
  156.568 ns (0 allocations: 0 bytes)
3

julia> @btime t($h)
  6.479 μs (2 allocations: 48 bytes)
3

julia> @btime m($h)
  3.611 μs (14 allocations: 784 bytes)
3

```

---

_[View the full topic](https://discourse.julialang.org/t/finding-the-number-of-arguments-of-an-anonymous-function/24394)._
