Array indexed by enum

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).

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

@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.

1 Like

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

1 Like

Oh, that’s nice: thanks.