# Array indexed by enum

**URL:** https://discourse.julialang.org/t/array-indexed-by-enum/56510
**Category:** General Usage
**Tags:** indexing, array, enum
**Created:** [March 4, 2021, 10:36pm UTC](https://discourse.julialang.org/t/array-indexed-by-enum/56510 "2021-03-04T22:36:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![grahamstark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamstark/32/5200_2.png) [@grahamstark](https://discourse.julialang.org/u/grahamstark)
#### Post date: [March 4, 2021, 10:36pm UTC](https://discourse.julialang.org/t/array-indexed-by-enum/56510/1 "2021-03-04T22:36:11Z")

</div>

Quick question: is there some implementation of an array that could use an enum as an index? Or would it be straightforward to implement one? Where would I start. (Using an enum as an array index is a common thing in languages I’m used to like Pascal, and it’s a useful thing in some contexts).

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 4, 2021, 10:46pm UTC](https://discourse.julialang.org/t/array-indexed-by-enum/56510/2 "2021-03-04T22:46:10Z")

</div>

How many enum’s do you have? That sounds more like a Dict object. You could do something like like:

```julia
@enum Fruit apple=1 orange=2 kiwi=3

Base.getindex(a::AbstractArray, i::Fruit) = getindex(a, Int(i))
Base.setindex!(a::AbstractArray, v, i::Fruit) = setindex!(a, v, Int(i))

a = zeros(3)
a[orange] = 57.0
a[orange]

```

Not sure if that would be considered type piracy.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 4, 2021, 10:54pm UTC](https://discourse.julialang.org/t/array-indexed-by-enum/56510/3 "2021-03-04T22:54:39Z")

</div>

> [@pixel27](#):
>
> Not sure if that would be considered type piracy.

It’s not type piracy as long as `Fruit` is a type that you defined, so this should be fine.

---

<div class="post-metadata">

### Author: ![grahamstark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamstark/32/5200_2.png) [@grahamstark](https://discourse.julialang.org/u/grahamstark)
#### Post date: [March 5, 2021, 12:21pm UTC](https://discourse.julialang.org/t/array-indexed-by-enum/56510/4 "2021-03-05T12:21:22Z")

</div>

Oh, that’s nice: thanks.
