# Optional macro invocation

**URL:** <https://discourse.julialang.org/t/optional-macro-invocation/18588>\
**Category:** General Usage\
**Created:** [December 12, 2018, 8:50am UTC](https://discourse.julialang.org/t/optional-macro-invocation/18588 "2018-12-12T08:50:41Z")\
**Posts on this page:** 1\
**Showing post:** 22

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [September 29, 2021, 8:33pm UTC](https://discourse.julialang.org/t/optional-macro-invocation/18588/22 "2021-09-29T20:33:07Z")

</div>

version 2 of my `maybethread` macro above, which optionally threads at compile time, is now hygienic:

```julia
macro maybethread(loop)
  if use_threads
    quote Threads.@threads $(Expr(loop.head,
                             Expr(loop.args[1].head, esc.(loop.args[1].args)...),
                             esc(loop.args[2]))); end
  else
    # @warn "running single threaded"
    quote $(esc(loop)); end
  end
end

```

i typically define `use_threads = Threads.nthreads()>1`.

thanks @[chakravala](https://discourse.julialang.org/u/chakravala) for [your post](https://discourse.julialang.org/t/calling-a-macro-from-within-a-macro-revisited/19680/12) which showed me how to do this.

figuring this out took a long time. maybe i’m stupid. maybe macros are hard. but i wholeheartedly agree with @c42f that we should [rethink them](https://github.com/JuliaLang/julia/issues/37691) for julia 2.0.

---

_[View the full topic](https://discourse.julialang.org/t/optional-macro-invocation/18588)._
