# Pretty macroexpand in Base?

**URL:** https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132
**Category:** General Usage
**Created:** [September 23, 2020, 12:20pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132 "2020-09-23T12:20:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [September 23, 2020, 12:20pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/1 "2020-09-23T12:20:31Z")

</div>

Whenever I talk to someone about macros, I always suggest they start by looking at the expansion of the macro. But the default expansion isn’t super readable because of LineNumberNode’s, gensyms, etc.

So I almost always end up using MacroTools.prettify:

```julia
MacroTools.prettify(@macroexpand @everywhere x = 1)

```

Should Base Julia have the equivalent of this composition in it? It seems like it could make macros more accessible.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [September 23, 2020, 12:25pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/2 "2020-09-23T12:25:39Z")

</div>

Agreed! I’ll just add that I sometimes prefer

```julia
 MacroTools.prewalk(rmlines, @macroexpand @everywhere x = 1)

```

to see the original variable names (even if they’re mangled) as this can give a much better idea of what’s going on for larger macros. A way to get the original symbols (unmangled) would be even better…

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [September 23, 2020, 1:10pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/3 "2020-09-23T13:10:42Z")

</div>

> [@tomerarnon](#):
>
> ` MacroTools.prewalk(rmlines, @macroexpand @everywhere x = 1)`

This looks equivalent to `Base.remove_linenums!` unless I am missing something? It should be pretty trivial to support a kwarg to `@macroexpand` that just remove linenumbers (similar to e.g. `@code_llvm` which has `debuginfo` as a kwarg).

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [September 23, 2020, 1:16pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/4 "2020-09-23T13:16:26Z")

</div>

Nice, I didn’t know there was already a way to do it in Base!

> [@fredrikekre](#):
>
> support a kwarg to `@macroexpand` that just remove linenumbers

This sounds great. Arguably the “brief” printout is more commonly needed than the verbose one, so maybe `brief=true` should even be the default?

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [September 23, 2020, 1:16pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/5 "2020-09-23T13:16:35Z")

</div>

The MacroTools version catches some line-numbers that the Base one misses:

```julia
julia> Base.remove_linenums!(@macroexpand1 @assert 1 == @view A[1])
:(if 1 == #= REPL[57]:1 =# @view(A[1])
      nothing
  else
      Base.throw(Base.AssertionError("1 == #= REPL[57]:1 =# @view(A[1])"))
  end)

julia> MacroTools.prewalk(rmlines, @macroexpand1 @assert 1 == @view A[1])
:(if 1 == @view(A[1])
      nothing
  else
      Base.throw(Base.AssertionError("1 == #= REPL[58]:1 =# @view(A[1])"))
  end)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 23, 2020, 1:22pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/6 "2020-09-23T13:22:05Z")

</div>

I think that the best solution is to make MacroTools a standard library (ideally after stdlib version numbers are decoupled from Julia proper).

It has a lot of useful functionality, and is practically indispensable for working with macros in Julia.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [September 23, 2020, 1:22pm UTC](https://discourse.julialang.org/t/pretty-macroexpand-in-base/47132/7 "2020-09-23T13:22:59Z")

</div>

Yes, it doesn’t strip out `LineNumberNode`s from `macrocall` expressions. I think `rmlines` actually replaces those with `nothing`.
