Enum tag collision

julia> @enum( Fruit, apple, orange, banana)
julia> @enum( Color, red, orange, blue)
ERROR: invalid redefinition of constant orange

I have many enums in a program at the same scope, and the program might even be autogenerated. What is the proper/simple way to do this so that each appears in its own namespace? Say, Fruit::orange, Color::orange ?

You could certainly do

module Fruit
    @enum(FruitType, apple, orange banana) 
end 
module Colors
    @enum(ColorType, red, orange, blue)
end 

Though I don’t think it was ever intended that you have hundreds of modules at a time, so I’m not sure if there are performance limitations involved.

On a more general note I’m not sure cluttering any namespace with a large number of procedurally generated names is a good idea.

2 Likes

I tried to stick all that into a macro for my_enum, and got the error

ERROR: syntax: "module" expression not at top level

so it looks like that approach will be limited to top level enum definitions … but it works for the current problem at hand. Thanks … though would like to have a general solution … or are enums top level only beasts?