Vector to @enum

Hello,

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

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 :slight_smile:

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.

2 Likes

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.

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.

1 Like

Hello @Henrique_Becker,

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

Best regards!

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

2 Likes

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!

Consider:

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.

1 Like

Hello,

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

Thank you very much for your help!