# Generating a macro call from a macro with the macro name as a symbol

**URL:** <https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [October 24, 2023, 5:34am UTC](https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335 "2023-10-24T05:34:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![awiersdorf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awiersdorf/32/205896_2.png) [@awiersdorf](https://discourse.julialang.org/u/awiersdorf)\
**Post date:** [October 24, 2023, 5:34am UTC](https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335/1 "2023-10-24T05:34:02Z")

</div>

I am trying to write a macro that expands into a call to another macro. Moreover, that other macro isn’t known statically—it’s determined at macro expansion time. I’m having difficulty constructing an AST with the macro name in a variable. Here’s what I’ve tried:

```julia
macro make_time_call(expr)
  time_macro = Symbol("@time")
  return Expr(:macrocall, [time_macro, LineNumberNode(1), 42])
end

@make_time_call 42

```

Gives me this error:

```julia
ERROR: LoadError: ArgumentError: "macrocall": too few arguments (expected 3)
Stacktrace:
 [1] include(fname::String)
   @ Base.MainInclude ./client.jl:478
 [2] top-level scope
   @ REPL[50]:1
in expression starting at /Users/ashton/…/tst.jl:6

```

Which seems really strange to me: no where am I _calling_ a function called `macrocall`—I’m just trying to create an AST node for a macro call. I’ve reproduced the three functions as best I can.

Then I tried using what looked like a solution [here](https://discourse.julialang.org/t/calling-a-macro-from-within-a-macro-revisited/19680/15):

```julia
macro make_time_call(expr)
  time_macro = Symbol("@time")
  return esc(:(@time $expr)) # works
end

@make_time_call 42

```

and that works as expected—I get the (short!) runtime for evaluating `42`. But I need to be able to switch what macro is called; I can’t just have `@time` sitting in there. So I tried this:

```julia
macro make_time_call(expr)
  time_macro = Symbol("@time")
  return esc(:($time_macro $expr))
end

@make_time_call 42

```

and I get this error:

```julia
ERROR: LoadError: UndefVarError: `$` not defined
Stacktrace:
 [1] top-level scope
   @ ~/…/tst.jl:6
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:478
 [3] top-level scope
   @ REPL[50]:1
in expression starting at /Users/ashton/…/tst.jl:6

```

Why does it think that `$` is a variable that’s not defined?!

How can I call a macro given a symbol?

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [October 24, 2023, 7:25am UTC](https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335/2 "2023-10-24T07:25:12Z")

</div>

Hello @awiersdorf, and welcome to the Julia community!

Your initial implementation was almost right, with one small mistake. The `Expr` constructor expects the arguments of the expression to be passed in splatted form, not as a single list. So with a simple modification, i.e., just remove the brackets, we can make your solution work:

```julia
macro make_time_call(expr)
  time_macro = Symbol("@time")
  return Expr(:macrocall, time_macro, LineNumberNode(1), 42)
end

julia> @make_time_call 42
  0.000001 seconds
42

```

That being said, I would probably pass ` __source__ ` instead of `LineNumbeNode(1)` as the first argument, like so:

```julia
macro make_time_call(expr)
  time_macro = Symbol("@time")
  return Expr(:macrocall, time_macro, __source__ , 42)
end

```

HTH!

---

<div class="post-metadata">

**Author:** ![awiersdorf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awiersdorf/32/205896_2.png) [@awiersdorf](https://discourse.julialang.org/u/awiersdorf)\
**Post date:** [October 24, 2023, 5:49pm UTC](https://discourse.julialang.org/t/generating-a-macro-call-from-a-macro-with-the-macro-name-as-a-symbol/105335/3 "2023-10-24T17:49:25Z")

</div>

That worked! Thank you so much!
