How to write a macro to export all instances of an enum type

A module has an enum type A and I want to export all its instances instances(A).
I want to write a macro @export_instances like

module MyModule
export A
@enum A a b c
@export_instances A
# `export a, b, c`
end # module

I have read Julia documentation’s metaprogramming page and Export enum - #4 by mathieu17g. I was able to implement the following function to return the expression to export all instances of an enum type T.

function export_instances(T)
    :(export $(Symbol.(instances(T))...))
end

Then, I have tried to write the desired macro but can’t. How do I?

Alex Arslan taught me on the Julia Slack

module Thing
macro exportinstances(enum)
    eval = GlobalRef(Core, :eval)
    return :($eval($__module__, Expr(:export, map(Symbol, instances($enum))...)))
end
@enum Fruit apple banana
@enuminstances Fruit
end # module
2 Likes

That was very useful, though I think the penultimate line of the solution should read:

@exportinstances Fruit