# How to call an imported (predefined) macro as a function on expressions?

**URL:** https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015
**Category:** General Usage
**Tags:** macros
**Created:** [September 11, 2021, 7:58am UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015 "2021-09-11T07:58:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![thisismygitrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisismygitrepo/32/28593_2.png) [@thisismygitrepo](https://discourse.julialang.org/u/thisismygitrepo)
#### Post date: [September 11, 2021, 7:58am UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/1 "2021-09-11T07:58:58Z")

</div>

Say that I installed a package that provides the macro `@pipe`, and I am making my own macro that uses that one. My question is, how can I call that macro as a function.

```julia
somehow / ideally: modified_expression = pipe(expression)

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 11, 2021, 10:40am UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/2 "2021-09-11T10:40:54Z")

</div>

I think I saw someone demonstrate the other day that you can call macros on expressions with `var"@somemacro"(expr)` to avoid executing the macro directly.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 11, 2021, 11:04am UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/3 "2021-09-11T11:04:13Z")

</div>

A macro is a syntactic transform. Given an expression, it transform it into another expression. This happens before the code is compiled, shortly after parsing your code into an Abstract Syntax Tree (called AST, represented by a julia `Expr` object). In that sense “calling” a macro doesn’t make sense, as it’s a syntactic transform that happens before functions are a thing (-ish - you can e.g. have your macro call a function that transform the expression for you and return the result of that function). In order to “call” a macro inside of another macro, you have to have your macro return an expression that contains the macro you want to “call” on the expression you want to call it on.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 11, 2021, 3:39pm UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/4 "2021-09-11T15:39:38Z")

</div>

Maybe you are looking for `macroexpand`.

---

<div class="post-metadata">

### Author: ![thisismygitrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisismygitrepo/32/28593_2.png) [@thisismygitrepo](https://discourse.julialang.org/u/thisismygitrepo)
#### Post date: [September 12, 2021, 11:43am UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/5 "2021-09-12T11:43:40Z")

</div>

I tried it. Indeed `var"@macroname"` looks like it is a method when typed in REPL. However, as I try to call it, I get this error

```julia
MethodError: no method matching var"@pipe"(::Expr)
Closest candidates are:
  var"@pipe"(::LineNumberNode, ::Module, ::Any) at C:\Users\Alex\.julia\packages\Pipe\5PIGG\src\Pipe.jl:99
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

```

---

<div class="post-metadata">

### Author: ![thisismygitrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisismygitrepo/32/28593_2.png) [@thisismygitrepo](https://discourse.julialang.org/u/thisismygitrepo)
#### Post date: [September 12, 2021, 11:48am UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/6 "2021-09-12T11:48:09Z")

</div>

Never realized that this is the intended use of `macroexpand`. I thought it was just for visual inspection. The name is misleading.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 12, 2021, 12:52pm UTC](https://discourse.julialang.org/t/how-to-call-an-imported-predefined-macro-as-a-function-on-expressions/68015/7 "2021-09-12T12:52:16Z")

</div>

> [@thisismygitrepo](#):
>
> The name is misleading.

It feels like a pretty accurate name to me in that it gives you the expanded form of the macro application. Any other suggestions?
