# Macro Question / Type Problem

**URL:** <https://discourse.julialang.org/t/macro-question-type-problem/7168>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [November 19, 2017, 12:09pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168 "2017-11-19T12:09:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 19, 2017, 12:09pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/1 "2017-11-19T12:09:17Z")

</div>

```julia
    function instrument(f1::EvaluatorFunction, f2::TrapFunction, ex::Expr)
      # returns an expr after modifying ex 
   end
    function instrument(trap_fun::TrapFunction, ex::Expr ;amod = current_module())
        instrument(EvaluatorFunction(amod.eval),trap_fun,ex)
    end

    function instrument(eval_fun::EvaluatorFunction,ex::Expr)
        trapless = Expr(:block, :(:T{false}) , ex)
        instrument(eval_fun,TrapFunction(deftrap),trapless)
    end

  macro instrument(fun,ex)
      esc(quote
            instrument($fun,$ex)
         end)
  end

#Example usage
@instrument eval function f(x)
 x
end

```

this code doesn’t work, because in the macro, ex is being passed as a function, not an Expr. is there way to pass ex as an Expr not an Function.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [November 19, 2017, 2:18pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/2 "2017-11-19T14:18:03Z")

</div>

Is

```julia
  macro instrument(fun,ex)
      esc(quote
            instrument($fun,$(QuoteNode(ex))
         end)
  end

```

what you want?

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 19, 2017, 3:09pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/3 "2017-11-19T15:09:38Z")

</div>

Hi,

I think what I am looking for is a way to dispatch on the macro argument fun.  
I also realized the instrument should return an expr and evaluated so macro code should look something like

```julia
  macro instrument(fun,ex)
      esc(quote
            $(instrument($fun,$(QuoteNode(ex)))
         end)
  end

```

But this doesn’t work either.

Thanks.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2017, 3:40pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/4 "2017-11-19T15:40:30Z")

</div>

It’s unclear from what you discribed,

1. When do you need `instrument` to execute (runtime or macro expansion time)
2. When do you want the “eval\_fun” to execute and what do you want it to do. In fact, you are likely using it incorrectly.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 19, 2017, 6:11pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/5 "2017-11-19T18:11:56Z")

</div>

1. the instrument call within the macro should be expanded to an expr within the macro expansion time.
2. the argument fun in the macro can be TrapFunction or EvaluatorFunction , and I want to dispatch to correct instrument function using the type of the fun argument.
3. eval\_fun function will be executed in runtime after macro expansion. The eval function is used to executing the expr injected into the expression at runtime.

```julia
#Example usage
@instrument evaluator function f(x)
 x
end

```

evaluator is a EvaluatorFunction type carrying a function confirming to type signature (amod::Module,ex) like eval.

Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2017, 8:19pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/6 "2017-11-19T20:19:42Z")

</div>

> [@Qiyamah](#):
>
> the instrument call within the macro should be expanded to an expr within the macro expansion time.

Then you should just do `instrument(...)` or `esc(instrument(...))` in the macro.

> [@Qiyamah](#):
>
> I want to dispatch to correct instrument function using the type of the fun argument.

That’s impossible.

> [@Qiyamah](#):
>
> eval\_fun function will be executed in runtime after macro expansion. The eval function is used to executing the expr injected into the expression at runtime.

That’s likely wrong. The execution of expression at runtime shouldn’t need a “eval function”. The macro return value will be executed and wrapping it in `eval` is almost garantee to be wrong.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 19, 2017, 8:46pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/7 "2017-11-19T20:46:44Z")

</div>

> [@](#):
>
> > [@Qiyamah](#):
> >
> > I want to dispatch to correct instrument function using the type of the fun argument.
> 
> That’s impossible.

care to explain why?

Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 19, 2017, 9:33pm UTC](https://discourse.julialang.org/t/macro-question-type-problem/7168/8 "2017-11-19T21:33:23Z")

</div>

That information is not available to the macro.
