# I get this wierd error when i try to implement to\_index with a struct

**URL:** <https://discourse.julialang.org/t/i-get-this-wierd-error-when-i-try-to-implement-to-index-with-a-struct/56503>\
**Category:** General Usage\
**Created:** [March 4, 2021, 8:55pm UTC](https://discourse.julialang.org/t/i-get-this-wierd-error-when-i-try-to-implement-to-index-with-a-struct/56503 "2021-03-04T20:55:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 4, 2021, 8:55pm UTC](https://discourse.julialang.org/t/i-get-this-wierd-error-when-i-try-to-implement-to-index-with-a-struct/56503/1 "2021-03-04T20:55:22Z")

</div>

I get this wierd error when i try to implement to\_index with a struct:  
`Base.to_index(s::State) = CartesianIndex(s.x,s.y)` errors with  
“getindex for Array{Float64,2} with types Tuple{CartesianIndex{2}} is not supported”

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614891282375000?thread_ts=1614891282.375000&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 4, 2021, 9:44pm UTC](https://discourse.julialang.org/t/i-get-this-wierd-error-when-i-try-to-implement-to-index-with-a-struct/56503/2 "2021-03-04T21:44:30Z")

</div>

`to_index` must return a a single `Int` index or an array of indices; you can’t return a `CartesianIndex`:

```julia
help?> Base.to_index
  to_index(A, i)

  Convert index i to an Int or array of indices to be used as an index into array A.

```

If you want to spit out more than one index spanning more than one dimension, you unfortunately need to use the more complicated `to_indices`… and not only do you need to handle _your_ index type, but you also need to play nicely with other indices that might appear alongside yours since we need to end up with a single argument list tuple.

You can probably do something similar to what `CartesianIndex` itself does — that is,

```nohighlight
@inline function Base.to_indices(A, inds, I::Tuple{State, Vararg{Any}})
    s = I[1]
    return to_indices(A, inds, (s.x ,s.y, tail(I)...))
end

```
