Generating @enum from string array

#1

Hey guys, I am rather new to Julia, but will try to do my best to explain my question. So basically I am making an enum (array?) called “Cat” which following variables:

@enum Cat begin
        Points
        Idp
        Vel
        Rhop
        Mass
        Press
        Vol
        Ace
        Vor
        Typ #Typ, since "Type" is illegal to assign
        Mk
    end

Where the integer values are non important since I use two different dicts like this:

    const searchString = Dict(Points => "POINTS ",Idp=>"POINT_DATA ",Vel=>"Vel ",Rhop=>"Rhop ",Mass=>"Mass ",Press => "Press ",Vol => "Vol ",Ace => "Ace ",Vor => "Vor ",Typ=>"Type ",Mk => "Mk ")
    const catType = Dict(Points => Float32,Idp=>Int32,Vel=>Float32,Rhop=>Float32,Mass=>Float32,Press =>Float32,Vol=>Float32,Ace=>Float32,Vor=>Float32,Typ=>Int8,Mk=>Int8)

So this works fine, but I want to improve it. So basically my question is:

Can I generate my enum Cat by using a String Array initially? If yes, how would I do that?

So in pseudo code code:

“Points” => enum Cat

Cat = Points

I hope it makes sense.

Kind regards

0 Likes

#2

Welcome to Julia-discourse!

I’m a bit confused by your two dicts. To get the string, just do string(Rhop) (that Points translates to "POINTS" seems a bit arbitrary). Then why do you associate types with your item? It they are needed, then maybe use a struct instead?

You could use eval, but that does not seem good. If you need to dynamically create an entity, use something which is made for this, like e.g. a Dict.

1 Like

#3

Thanks for the explanation, didn’t know structs existed.

I just hoped that it would be possible to dynamically make @enums since I like to work that way, but dict might be the best alternative

Kind regards

0 Likes