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

**URL:** <https://discourse.julialang.org/t/trying-to-understand-method-specializations-involving-cartesianindices-in-julias-multidimensional-jl/65294>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [July 26, 2021, 8:28am UTC](https://discourse.julialang.org/t/trying-to-understand-method-specializations-involving-cartesianindices-in-julias-multidimensional-jl/65294 "2021-07-26T08:28:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 26, 2021, 8:28am UTC](https://discourse.julialang.org/t/trying-to-understand-method-specializations-involving-cartesianindices-in-julias-multidimensional-jl/65294/1 "2021-07-26T08:28:53Z")

</div>

Julia’s multidimensional.jl [defines the methods](https://github.com/JuliaLang/julia/blob/f7f46af8ff39a1b4c7000651c680058e9c0639f5/base/multidimensional.jl#L384-L386)

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 26, 2021, 8:32am UTC](https://discourse.julialang.org/t/trying-to-understand-method-specializations-involving-cartesianindices-in-julias-multidimensional-jl/65294/2 "2021-07-26T08:32:44Z")

</div>

Often you see repetition like this to avoid method ambiguities.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 26, 2021, 10:16am UTC](https://discourse.julialang.org/t/trying-to-understand-method-specializations-involving-cartesianindices-in-julias-multidimensional-jl/65294/3 "2021-07-26T10:16:50Z")

</div>

> [@jishnub](#):
>
> ```julia
> ndims(::Type{CartesianIndices{N}}) where {N} = N
> ndims(::Type{CartesianIndices{N,TT}}) where {N,TT} = N
> 
> ```

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?
