# Vector to @enum

**URL:** <https://discourse.julialang.org/t/vector-to-enum/79907>\
**Category:** General Usage\
**Tags:** question, array, arrays, vector, enum\
**Created:** [April 23, 2022, 5:21pm UTC](https://discourse.julialang.org/t/vector-to-enum/79907 "2022-04-23T17:21:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [April 23, 2022, 5:21pm UTC](https://discourse.julialang.org/t/vector-to-enum/79907/1 "2022-04-23T17:21:30Z")

</div>

Hello,

I would like to know how to convert a vector into @enum. I have tried:

```julia
AS =[Action(τ,ρ,χ,ω) for τ in 0:5 for ρ in 0:5 for χ in 0:5 for ω in 0:5]

for i in 1:1296
    action = AS[i]
    @enum Actions action
end

```

But it does not work.

Could you please help me? Thank you so much 🙂

---

<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:** [April 23, 2022, 7:41pm UTC](https://discourse.julialang.org/t/vector-to-enum/79907/2 "2022-04-23T19:41:18Z")

</div>

What exactly are you trying to achieve with `@enum Actions action`? There are multiple issues with that line.

1. You cannot grow an Enum, it needs to be initialized with all the values, so calling `@enum` inside a loop makes little sense.
2. `@enum` is a macro, in other words, it takes code and outputs code. If you use `@macroexpand` in front of it you can see it creates a new primitive type `Actions` and define `action` to be a constant of value zero of these new primitive type. So, it does not look at what is inside `action` as a variable, it takes `action` as the code/string/expression “action”, independently of what is inside. In fact, there should be nothing inside, as all macro parameters should be unused identifiers.
3. What means making an `Action` object a value of the `Actions` enum? Enums are basically a wrapper for non-negative numbers, so numbers identifying different things (e.g., car model number and car color, for example) are not confused and mixed. Enums are not made to wrap arbitrary composite values as your `Action` objects.

I am not sure if you can convert a vector into a `Enum` without the use of `eval` (that has its own problems). But I think you probably are misusing `Enum`, and whatever problem you have this is not the way to solve it.

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [April 23, 2022, 8:36pm UTC](https://discourse.julialang.org/t/vector-to-enum/79907/3 "2022-04-23T20:36:08Z")

</div>

Hello and thank you so much for your answer, let me explain you what I am trying to do.

In need to ge the indexes of the actions vector.

---

<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:** [April 24, 2022, 7:58pm UTC](https://discourse.julialang.org/t/vector-to-enum/79907/4 "2022-04-24T19:58:18Z")

</div>

I maybe will need a bit more information than that.

Why do you not use the numbers from 1 to 1296 as you are using in your example (or more generally, either `collect(keys(AS))` or `1:length(AS)`)? What is stopping you from doing that.

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [April 25, 2022, 7:55am UTC](https://discourse.julialang.org/t/vector-to-enum/79907/5 "2022-04-25T07:55:44Z")

</div>

Hello @Henrique_Becker,

Thank you so much for your help, finally I did `collect(keys(AS))`.

Best regards!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 25, 2022, 8:05am UTC](https://discourse.julialang.org/t/vector-to-enum/79907/6 "2022-04-25T08:05:35Z")

</div>

> [@clrescobar](#):
>
> In need to ge the indexes of the actions vector.

> [@Henrique\_Becker](#):
>
> or more generally, either `collect(keys(AS))` or `1:length(AS)` )?

I personally prefer `eachindex(AS)` - it’s faster & more generic than explicitly `collect`ing a (usually) compact representation of the same thing.

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [April 25, 2022, 8:09am UTC](https://discourse.julialang.org/t/vector-to-enum/79907/7 "2022-04-25T08:09:36Z")

</div>

> [@Sukera](#):
>
> `eachindex(AS)`

Hello @Sukera,

First of all thank you for answering my question, could you please tell me how to know wich of them is better? I mean regarding performance.

Thank you!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 25, 2022, 8:14am UTC](https://discourse.julialang.org/t/vector-to-enum/79907/8 "2022-04-25T08:14:57Z")

</div>

Consider:

```julia
julia> using Random

julia> a = shuffle!([1,2,3,4,5,6])
6-element Vector{Int64}:
 4
 5
 1
 2
 3
 6

julia> eachindex(a)
Base.OneTo(6)

julia> collect(keys(a))
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6

```

`Base.OneTo(6)` is tiny, it only stores the maximum index and iterating over this already gives you each index of `a` (in this case starting with `1`). `collect(keys(a))` allocates a _new vector_ of the same size as `a`, containing each individual index in memory. However, you don’t actually need to store every index ahead of time, especially if you’re only iterating over them. Storing every possible index physically in memory is quite wasteful, especially when the incoming array is large.

---

<div class="post-metadata">

**Author:** ![clrescobar](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Post date:** [April 25, 2022, 8:23am UTC](https://discourse.julialang.org/t/vector-to-enum/79907/9 "2022-04-25T08:23:48Z")

</div>

> [@Sukera](#):
>
> `Base.OneTo(6)` is tiny, it only stores the maximum index and iterating over this already gives you each index of `a` (in this case starting with `1` ). `collect(keys(a))` allocates a _new vector_ of the same size as `a` , containing each individual index in memory. However, you don’t actually need to store every index ahead of time, especially if you’re only iterating over them. Storing every possible index physically in memory is quite wasteful, especially when the incoming array is large.

Hello,

Perfect, I’ll change my code to be more efficient.

Thank you very much for your help!
