Export enum

Hi

I am defining an enum such as @enum myenum enumname1 enumname2 in a module using Julia 0.6. it seems I have to explicitly export both enum type name(myenum) and enum elements(enumname1,enumname2). I know this way is more explicit, but usually we want all elements of an enum type be exported too. is there a way to just do export myenum so that type name and all its elements be exported, or should this behavior be the default for export enum?

Alex

2 Likes

Here’s a macro that does it:

macro exported_enum(name, args...)
    esc(quote
        @enum($name, $(args...))
        export $name
        $([:(export $arg) for arg in args]...)
        end)
end

@exported_enum fruit apple banana
6 Likes

this macro is great, should be included in the base. thanks sharing.

1 Like

In order to deal with the case where you specify the value of each instances, you may use:

julia> module A
           macro exported_enum(T, syms...)
               return esc(quote
                   @enum($T, $(syms...))
                   export $T
                   for inst in Symbol.(instances($T))
                       eval($(Expr(:quote, :(export $(Expr(:$, :inst))))))
                   end
               end)
           end
           @exported_enum fruit apple=2 banana=3
       end
Main.A

julia> using Main.A

julia> fruit
Enum fruit:
apple = 2
banana = 3
2 Likes