# Determine whether any one-argument method exists

**URL:** <https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 22, 2019, 8:34am UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917 "2019-01-22T08:34:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [January 22, 2019, 8:34am UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/1 "2019-01-22T08:34:03Z")

</div>

I would like to check whether a function `f` has any one-argument methods. Since `Union{} <: [any type]`, I would argue that the following should work:

```julia
hasmethod(f, Tuple{Union{}})

```

It seems to only work if `f` has a one-argument method with abstract argument, however:

```julia
julia> hasmethod((::Integer)->nothing,Tuple{Union{}})
true

julia> hasmethod((::Int)->nothing,Tuple{Union{}})
false

```

This seems like a bug in the language (albeit arguable not the most severe one). Is there a workaround for this?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2019, 1:23pm UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/2 "2019-01-22T13:23:51Z")

</div>

> [@ettersi](#):
>
> This seems like a bug in the language (albeit arguable not the most severe one). Is there a workaround for this?

It’s not a bug. ~~`hasmethod(f, Tuple{Union{}})` or~~ `hasmethod(f, Tuple{Any})` are asking whether `f` has a method whose argument type is `Any` or any _supertype_ of `Any`, not any _subtype_ which is what you are hoping for.

For example, `hasmethod(f, Tuple{Real})` _will_ succeed if there is an `f(x::Real)` method, but also if there is an `f(x::Number)` or `f(x::Any)` method. It _won’t_ succeed if there is only an `f(x::Int)` method.

One thing you could do to check whether `f` has a single-argument method would be to check `any(m -> m.nargs == 2, methods(f))`, although this relies on the undocumented internal field `nargs` of the `Method` type. (`m.nargs == 2` seems to be correct for to test for 1 argument, since the function itself is apparently counted as an “argument” here.)

_Update:_ My bad, I was confusing `Union{}` with `Any`. As was pointed out below, `Union{}` is a _supertype_ of every type, so it seems like it should work.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [January 22, 2019, 1:31pm UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/3 "2019-01-22T13:31:23Z")

</div>

> [@stevengj](#):
>
> `hasmethod(f, Tuple{Union{}})` or `hasmethod(f, Tuple{Any})` are asking whether `f` has a method whose argument type is `Any` or any _supertype_ of `Any` , not any _subtype_ which is what you are hoping for.

I’m not sure I understand. `hasmethod(f,Tuple{T})` asks whether there is a method `f(::U)` such that `T <: U`, correct? And the [documentation](https://docs.julialang.org/en/v1/manual/types/index.html) says

> all types are supertypes of `Union{}`

So I don’t see why the way I use `hasmethod` should not be correct.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 22, 2019, 1:31pm UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/4 "2019-01-22T13:31:26Z")

</div>

> [@stevengj](#):
>
> whether `f` has a method whose argument type is `Any` or any _supertype_ of `Any`

Which is why I thought that the approach would work, since

```julia
julia> f(::Int) = 1
f (generic function with 1 method)

julia> hasmethod(f, Tuple{Union{}})
false

julia> Union{} <: Int
true

```

ie `Int` is a supertype of `Union{}`, as all things are.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2019, 1:51pm UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/5 "2019-01-22T13:51:48Z")

</div>

> [@Tamas\_Papp](#):
>
> ie `Int` is a supertype of `Union{}` , as all things are.

Whoops, right, my bad; I got confused between `Union{}` (the bottom type) and `Any` (the top type).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 22, 2019, 1:53pm UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/6 "2019-01-22T13:53:48Z")

</div>

Frankly, I think this is a bug. If you open an issue, please link it here.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [January 23, 2019, 1:20am UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/7 "2019-01-23T01:20:17Z")

</div>

[https://github.com/JuliaLang/julia/issues/30808](https://github.com/JuliaLang/julia/issues/30808)

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [August 21, 2019, 11:29pm UTC](https://discourse.julialang.org/t/determine-whether-any-one-argument-method-exists/19917/8 "2019-08-21T23:29:55Z")

</div>

Not exactly the same, but related

```julia
f(x::T) where T = 1
hasmethod(f, Tuple{T} where T)

```

crash the julia repl on julia 1.1.1, windows 7
