# 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:** 2

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [December 12, 2018, 10:23am UTC](https://discourse.julialang.org/t/optional-macro-invocation/18588/2 "2018-12-12T10:23:40Z")

</div>

Does this help ?

```julia
macro maybeasync(asyncflag, expr)
    quote
        if $(esc(asyncflag))
            @async $(esc(expr))
        else
            $(esc(expr))
        end
    end
end

function docos(flag)
     @maybeasync flag cos(1)
end

```

```julia
julia> docos(true)
Task (done) @0x00007f49b7e4a4a0

julia> docos(false)
0.5403023058681398

```

EDIT: There’s probably a cheaper way to do it, in terms of code generation.

---

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