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

**URL:** https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137
**Category:** General Usage
**Tags:** question, macros, metaprogramming, enum
**Created:** [December 15, 2021, 11:35am UTC](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137 "2021-12-15T11:35:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [December 15, 2021, 11:35am UTC](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137/1 "2021-12-15T11:35:02Z")

</div>

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

```julia
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](https://docs.julialang.org/en/v1/manual/metaprogramming/) and [Export enum - #4 by mathieu17g](https://discourse.julialang.org/t/export-enum/5396/4). I was able to implement the following function to return the expression to export all instances of an enum type `T`.

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

```

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

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [December 20, 2021, 4:31am UTC](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137/2 "2021-12-20T04:31:36Z")

</div>

Alex Arslan taught me on the Julia Slack

```julia
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

```

---

<div class="post-metadata">

### Author: ![PGS62](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgs62/32/207030_2.png) [@PGS62](https://discourse.julialang.org/u/PGS62)
#### Post date: [September 22, 2022, 8:45am UTC](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137/3 "2022-09-22T08:45:58Z")

</div>

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

```julia
@exportinstances Fruit

```
