Hello, I think I understand the following, but is it actually expected?
julia> hasmethod(pairs, Tuple{UnionAll})
true
julia> pairs(Type)
ERROR: MethodError: no method matching keys(::Type{Type})
IIUC pairs is defined for that input, but it should not actually be ran. I was relying on hasmethod to figure whether arbitrary input could be iterated upon using pairs, but maybe I should ask "forgiveness rather than permission"?
I'm not sure I understand the question. pairs is applicable to any tuple, but your second call doesn't have a tuple argument at all.
IIUC the tuple in hasmethod is just here to list the requested argument types. Here one single argument Type::UnionAll thus Tuple{UnionAll} (edited/fixed in the above)
I'm still confused but for what it's worth:
julia> pairs((Type,))
pairs(::Tuple{UnionAll}) with 1 entry:
1 => Type
My point is that I would expect pairs(Type) to succeed because hasmethod(pairs, Tuple{UnionAll}) returns true. It's failing though. Not because the method is undefined, but because some internal callee keys seems undefined.
(Actually I would rather expect hasmethod(pairs, Tuple{UnionAll}) to return false instead because pairs(Type) fails.)
To clarify: the following is less suprising:
julia> hasmethod(sqrt, Tuple{Int}) # One argument of type `Int`.
true
julia> sqrt(5) # Doesn't fail.
2.23606797749979
Part of my confusion was that I didn't read (or remember) the hasmethod docstring well enough, sorry about that.
I think the general problem is that pairs must work for any collection or iterable, but there's no abstract type for all of those, so pairs needs to dispatch on Any. If you call it with other input it will error and the question is mostly how gracefully it does that.
I see. So there is no way I can query ::Any for being "iterable"?
I'm not sure how robust it is, but trying hasmethod on iterate is probably more informative.
It is, but I expect to iterate over matrices using pairs not iterate (which would traverse them linearly) :\
I find it difficult to write generic code then ^ ^"
Regarding your sqrt example, sqrt(-5) does error despite having a method defined.
Indeed, this is why I sort of understand the above. I also understand how hasmethod is not a reliable way to "duck-type" input.
Note that sqrt(-5) fails because of the input runtime value, whereas pairs(Type) fails because of the input static type.
I wish there would be a way to request whether the call is at least statically correct.
Like a deephasmethod 0:)
JET can do that kind of analysis.
julia> JET.@report_call sqrt(-5)
No errors detected
julia> JET.@report_call pairs(Type)
═════ 1 possible error found ═════
┌ pairs(collection::Type{Type}) @ Base ./abstractdict.jl:178
│ no matching method found `keys(::Type{Type})`: keys(collection::Type{Type})
└────────────────────────────────────────────────────────────────────────────
That seems.. impressive! Do you think it's resolved at compile time?
Or that we could actually implement deephasmethod in terms of that @report_call?
The whole point of JET is to do static analysis but it has some limitations.
Last updated: Nov 21 2026 at 12:52 UTC