# Struct with array and dict of symbols: index array by symbol

**URL:** <https://discourse.julialang.org/t/struct-with-array-and-dict-of-symbols-index-array-by-symbol/9040>\
**Category:** General Usage\
**Created:** [February 13, 2018, 9:03pm UTC](https://discourse.julialang.org/t/struct-with-array-and-dict-of-symbols-index-array-by-symbol/9040 "2018-02-13T21:03:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EP-Guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ep-guy/32/3442_2.png) [@EP-Guy](https://discourse.julialang.org/u/EP-Guy)\
**Post date:** [February 13, 2018, 9:03pm UTC](https://discourse.julialang.org/t/struct-with-array-and-dict-of-symbols-index-array-by-symbol/9040/1 "2018-02-13T21:03:56Z")

</div>

I have defined a struct:

```julia
struct State{T} <: AbstractArray{T,1}
    fields::Dict{Symbol, UnitRange{Int64}}
    arr::Array{T}
end

```

that might be initialized by something like

```julia
state0 = State(Dict([(:r, 1:3), (:v, 4:6)]), [10; 11; 12; 13; 14; 15])

```

I want to be able to do `state0[:r]` and get back `[10; 11; 12]`.

I’ve tried defining

```julia
Base.getindex(S::State{T}, ind::Symbol) where {T} = S.arr[S.fields[ind]]

```

but I get `ERROR: ArgumentError: invalid index: r`.

Any fixes or suggestions on how to do this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 13, 2018, 9:06pm UTC](https://discourse.julialang.org/t/struct-with-array-and-dict-of-symbols-index-array-by-symbol/9040/2 "2018-02-13T21:06:28Z")

</div>

I literally copied your code and got:

```julia
julia> state0[:r]
3-element Array{Int64,1}:
 10
 11
 12

```

---

<div class="post-metadata">

**Author:** ![EP-Guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ep-guy/32/3442_2.png) [@EP-Guy](https://discourse.julialang.org/u/EP-Guy)\
**Post date:** [February 13, 2018, 9:42pm UTC](https://discourse.julialang.org/t/struct-with-array-and-dict-of-symbols-index-array-by-symbol/9040/3 "2018-02-13T21:42:57Z")

</div>

Wow, you’re right. I’m not sure what the problem was - I had already tried restarting the REPL, but I did so again and now it works.
