# Surprising Type Error

**URL:** https://discourse.julialang.org/t/surprising-type-error/51943
**Category:** New to Julia
**Created:** [December 16, 2020, 6:09pm UTC](https://discourse.julialang.org/t/surprising-type-error/51943 "2020-12-16T18:09:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jsundram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsundram/32/20176_2.png) [@jsundram](https://discourse.julialang.org/u/jsundram)
#### Post date: [December 16, 2020, 6:09pm UTC](https://discourse.julialang.org/t/surprising-type-error/51943/1 "2020-12-16T18:09:42Z")

</div>

I ran into the following unexpected error using and array of `CartesianIndex`:

```julia
julia> c = CartesianIndex[]
CartesianIndex[]

julia> push!(c, CartesianIndex(1, 2))
1-element Array{CartesianIndex,1}:
 CartesianIndex(1, 2)

julia> M = [1 2; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> M[c]
ERROR: ArgumentError: unable to check bounds for indices of type CartesianIndex{2}
Stacktrace:
 [1] checkindex(::Type{Bool}, ::Base.OneTo{Int64}, ::CartesianIndex{2}) at ./abstractarray.jl:561
 [2] checkindex at ./abstractarray.jl:576 [inlined]
 [3] checkbounds_indices at ./abstractarray.jl:532 [inlined]
 [4] checkbounds at ./multidimensional.jl:544 [inlined]
 [5] checkbounds at ./abstractarray.jl:506 [inlined]
 [6] _getindex at ./multidimensional.jl:742 [inlined]
 [7] getindex(::Array{Int64,2}, ::Array{CartesianIndex,1}) at ./abstractarray.jl:1060
 [8] top-level scope at REPL[44]:1

```

However, if I iterate over the same array, I get what I expected:

```julia
julia> for x in c
           println(M[x])
       end
2

```

After some head scratching (and possibly some cursing), I amended the original declaration of c to `c = CartesianIndex{2}[]`, which fixed the problem – `M[c] ` gives the expected result of `2`.

I’m a bit confused by this behavior – can someone explain what’s going on here?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [December 16, 2020, 7:06pm UTC](https://discourse.julialang.org/t/surprising-type-error/51943/2 "2020-12-16T19:06:27Z")

</div>

Probably there is a `getindex` method for `Array{T, N}` that takes `CartesianIndex{N}[]` (i.e., a `Vector` of concretely typed indexes with the right dimensions) but if the `Vector` is not concretely typed then it probably enters a fallback that errors after.

---

<div class="post-metadata">

### Author: ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)
#### Post date: [December 21, 2020, 5:03pm UTC](https://discourse.julialang.org/t/surprising-type-error/51943/3 "2020-12-21T17:03:54Z")

</div>

Thanks for posting this. Saved me some definite cursing. Not only is this catch/bug obscure, but the error message is almost Kafkaesque.
