Stream: helpdesk (published)

Topic: pairs(Type)


view this post on Zulip iago-lito (Nov 12 2026 at 14:10):

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"?

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 14:34):

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.

view this post on Zulip iago-lito (Nov 12 2026 at 14:36):

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)

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 14:38):

I'm still confused but for what it's worth:

julia> pairs((Type,))
pairs(::Tuple{UnionAll}) with 1 entry:
  1 => Type

view this post on Zulip iago-lito (Nov 12 2026 at 14:39):

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.

view this post on Zulip iago-lito (Nov 12 2026 at 14:40):

(Actually I would rather expect hasmethod(pairs, Tuple{UnionAll}) to return false instead because pairs(Type) fails.)

view this post on Zulip iago-lito (Nov 12 2026 at 14:41):

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

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 14:49):

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.

view this post on Zulip iago-lito (Nov 12 2026 at 14:50):

I see. So there is no way I can query ::Any for being "iterable"?

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 14:53):

I'm not sure how robust it is, but trying hasmethod on iterate is probably more informative.

view this post on Zulip iago-lito (Nov 12 2026 at 14:53):

It is, but I expect to iterate over matrices using pairs not iterate (which would traverse them linearly) :\

view this post on Zulip iago-lito (Nov 12 2026 at 14:54):

I find it difficult to write generic code then ^ ^"

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 14:54):

Regarding your sqrt example, sqrt(-5) does error despite having a method defined.

view this post on Zulip iago-lito (Nov 12 2026 at 14:55):

Indeed, this is why I sort of understand the above. I also understand how hasmethod is not a reliable way to "duck-type" input.

view this post on Zulip iago-lito (Nov 12 2026 at 14:55):

Note that sqrt(-5) fails because of the input runtime value, whereas pairs(Type) fails because of the input static type.

view this post on Zulip iago-lito (Nov 12 2026 at 14:58):

I wish there would be a way to request whether the call is at least statically correct.

view this post on Zulip iago-lito (Nov 12 2026 at 14:59):

Like a deephasmethod 0:)

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 15:08):

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})
└────────────────────────────────────────────────────────────────────────────

view this post on Zulip iago-lito (Nov 12 2026 at 15:09):

That seems.. impressive! Do you think it's resolved at compile time?

view this post on Zulip iago-lito (Nov 12 2026 at 15:10):

Or that we could actually implement deephasmethod in terms of that @report_call?

view this post on Zulip Gunnar Farnebäck (Nov 12 2026 at 15:15):

The whole point of JET is to do static analysis but it has some limitations.


Last updated: Nov 21 2026 at 12:52 UTC