`hasmethod` with a "wildcard"

I’d like to check if a type has iterate defined. Of course, iterate needs two methods – iterate(::T) and iterate(::T, state).

julia> hasmethod(iterate, (Vector{Int}, Any))
true

julia> hasmethod(iterate, (Tuple{Int}, Any))
false # huh?

julia> hasmethod(iterate, (Tuple{Int}, Int))
true # Oooh...

I want to know if there is a two-argument method for iterate where the first argument is of some type T, but I don’t care what the second type is. I know that I could call methods(iterate) and then filter the returned list, but is there some way for me to called hasmethod(iterate, (Tuple{Int}, *))?

I think methods can be used here:

methodlist = methods(iterate, (Tuple{Int}, Any))
isempty(methodlist) # false
2 Likes

Ah! Brilliant!

1 Like