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

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.

somehow / ideally: modified_expression = pipe(expression)

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.

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.

Maybe you are looking for macroexpand.

1 Like

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

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
 [1] top-level scope
   @ REPL[4]:1

Never realized that this is the intended use of macroexpand. I thought it was just for visual inspection. 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?