Trying to understand method specializations involving CartesianIndices in julia's multidimensional.jl?

Julia’s multidimensional.jl defines the methods

ndims(::Type{CartesianIndices{N}}) where {N} = N
ndims(::Type{CartesianIndices{N,TT}}) where {N,TT} = N

I wonder if these may not be written concisely as

ndims(::Type{<:CartesianIndices{N}}) where {N} = N

?
is there a reason for the explicit specialization in the methods above? Is there a performance gain expected from this? Is this a pattern that users might consider using for their own code?

Often you see repetition like this to avoid method ambiguities.

1 Like

Adding to the question:

I found by chance these days that an instance of the second type will be dispatched to the first function if the second is not defined.

It felt very strange at first sight. Is that something actually supported as a language standard, or is it an implementation detail?