# How to make macro that calls expr asyncly?

**URL:** https://discourse.julialang.org/t/how-to-make-macro-that-calls-expr-asyncly/9559
**Category:** General Usage
**Tags:** question
**Created:** [March 7, 2018, 4:11am UTC](https://discourse.julialang.org/t/how-to-make-macro-that-calls-expr-asyncly/9559 "2018-03-07T04:11:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [March 7, 2018, 4:11am UTC](https://discourse.julialang.org/t/how-to-make-macro-that-calls-expr-asyncly/9559/1 "2018-03-07T04:11:46Z")

</div>

let’s say you have a function (in the Main scope):

```julia
function foo()
  for cur_index in 99:-1:0
    print("$(cur_index) bottles of beer on the wall. ")
    println("$(cur_index) bottles of beer.")
    println("Take one down. Pass it around.")
    sleep(0.5)
  end
end

```

and you want to run another function at the same time through some macro `buzz` (described later):

```julia
module Fizz

  function bar()
    cows_come_home = false
    @async while !cows_come_home
      cur_rand = rand(0:99)
      cows_come_home = ( cur_rand < 5 )

      println("$(cur_rand) bottles of beer!")
      sleep(cur_rand/100)
    end
  end

  # macro buzz defined below
  # (just a wrapper for bar func)

  export @buzz

end

```

* * *

if you wanted to create a macro, that:

- calls `bar` no matter what and
- calls some expression `async`-ly

How would you do it?

The following macro gives an undefined error:

```julia
module Fizz

  # function bar defined above
  # (just wrapped by buzz)

  macro buzz(cur_expr::Expr)
    bar()

    cur_expression = Expr(
      :macrocall,
      Symbol("@async"),
      cur_expr
    )

    return cur_expression
  end

  export @buzz

end

```

when making the call:

```julia
julia> using Fizz
julia> @buzz foo()

20 bottles of beer!ERROR (unhandled task failure): UndefVarError: foo not defined
Stacktrace:
 [1] (::##1#2)() at ./task.jl:335

Task (failed) @0x00000001246d3610
UndefVarError: foo not defined
(::##1#2)() at ./task.jl:335

julia> 74 bottles of beer!
2 bottles of beer!
```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 7, 2018, 4:38am UTC](https://discourse.julialang.org/t/how-to-make-macro-that-calls-expr-asyncly/9559/2 "2018-03-07T04:38:29Z")

</div>

You’re quite close, but you need to change two things:

1. You probably don’t want to call `bar()` inside the body of the macro, since that will cause `bar()` to be called when the macro is expanded instead of where it occurs in the code. Just put the call inside the returned expression.
2. You need to escape `cur_expr` since otherwise it will be sanitized by the macro hygiene system. User input must be `esc`ed exactly once.

```julia
module Fizz

function bar()
    cows_come_home = false
    @async while !cows_come_home
      cur_rand = rand(0:99)
      cows_come_home = ( cur_rand < 5 )

      println("$(cur_rand) bottles of beer!")
      sleep(cur_rand/100)
    end
  end

macro buzz(expr::Expr)
    quote
        bar()
        $(Expr(:macrocall,
            Symbol("@async"),
            esc(expr)
        ))
    end
end

end

```

usage:

```julia
Main> Fizz.@buzz foo()
11 bottles of beer!99 bottles of beer on the wall.
Task (runnable) @0x00007faefc172230
99 bottles of beer.

Main> Take one down. Pass it around.
Main> 20 bottles of beer!
13 bottles of beer!
56 bottles of beer!
98 bottles of beer on the wall. 98 bottles of beer.
Take one down. Pass it around.
<truncated>

```

---

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [March 7, 2018, 10:06pm UTC](https://discourse.julialang.org/t/how-to-make-macro-that-calls-expr-asyncly/9559/3 "2018-03-07T22:06:53Z")

</div>

lol. that’s too fun 😛
