# Calling a macro from another macro

**URL:** <https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708>\
**Category:** Help & Support\
**Created:** [February 13, 2020, 5:52pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708 "2020-02-13T17:52:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [February 13, 2020, 5:52pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708/1 "2020-02-13T17:52:43Z")

</div>

Now that I am learning how to write simple macros, is it possible to call macro B from macro A  
with some arguments ?

```auto
macro macro_B(arg1,arg2)
  def example()
    x = {{arg1}}({{arg2}})
  end    
end

macro macro_A(name,arg1,arg2)

  def {{name}} : Array({{arg1}})
    ... some code ...
    # call macro_B
    macro_B arg1, arg2
  end	
end

macro_A "abc", 123, 456

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [February 13, 2020, 6:15pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708/2 "2020-02-13T18:15:36Z")

</div>

Of course. You just need to expand the arguments in the calling macro’s body like with any other call: `macro_B {{arg1}}, {{arg2}}`

---

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [February 13, 2020, 6:36pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708/3 "2020-02-13T18:36:44Z")

</div>

I tried the following at [https://play.crystal-lang.org/](https://play.crystal-lang.org/) (0.32.1)  
It seems macro\_B is not called but rather the text “macro\_B {{arg1}}, {{arg2}}” is echoed.

Note: This is just an example and not valid crystal code.

```auto
macro macro_B(arg1,arg2)
  def example()
    x = {{arg1}}({{arg2}})
  end
end
 
macro macro_A(name,arg1,arg2)
 
  def {{name}} : Array({{arg1}})
    x = 1
    # call macro_B
    macro_B {{arg1}}, {{arg2}}
  end
  {{debug}}
end
 
macro_A abc, 123, 456

```

and the output

```auto
def abc : Array(123)
  x = 1
  # call macro_B
  macro_B 123, 456
end

```

---

<div class="post-metadata">

**Author:** ![anykeyh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/anykeyh/32/726_2.png) [@anykeyh](https://forum.crystal-lang.org/u/anykeyh)\
**Post date:** [February 13, 2020, 6:43pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708/4 "2020-02-13T18:43:27Z")

</div>

This is correct. Then the block is evaluated again by the macro system and `macro_B` is going to be expanded ;-)

---

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [February 13, 2020, 8:25pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708/5 "2020-02-13T20:25:48Z")

</div>

So basically macro\_B is not called and expanded ie one cannot call macros from macros.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 13, 2020, 8:57pm UTC](https://forum.crystal-lang.org/t/calling-a-macro-from-another-macro/1708/6 "2020-02-13T20:57:06Z")

</div>

That’s correct. There’s no way to call a macro from another macro. You can only have a macro generate code that calls another macro.
