Skip to content

Instantly share code, notes, and snippets.

@Luegg
Last active December 30, 2015 03:09
Show Gist options
  • Save Luegg/7767312 to your computer and use it in GitHub Desktop.
Save Luegg/7767312 to your computer and use it in GitHub Desktop.

Print Raw ASTs in the REPL

Sometimes it is just useful to see the typechecked AST of a scala expression.

Macros to the rescue:

import scala.reflect.macros.Context
import scala.language.experimental.macros

def astImpl(c: Context)(expr: c.Expr[Any]): c.Expr[Unit] = {
  import c.universe._
  println(showRaw(expr.tree))
  reify{}
}

def ast(expr: Any) = macro astImpl

Copy the snippet to the REPL and use it like:

scala> ast{
     | println(1)
     | }
Apply(Select(Select(This(newTypeName("scala")), scala.Predef), newTermName("println")), List(Literal(Constant(1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment