I'm looking at alternatives to -print
or javap
as a way of figuring out what the compiler is doing in Scala. With the new reflection/macros library, reify
seems a good candidate for that, as shown in retronym's macrocosm's desugar
. It even shows how one used to do that, pre-M4.
So the question is, what's the shortest/easiest thing I can type on Scala's REPL to get the AST for an expression, post-Scala 2.10.0-M4?
A lot of things previously defined in package
scala.reflect.mirror
have moved toscala.reflect.runtime.universe
:Furthermore it is possible to check if a string containing some Scala code is a valid Scala expression and - even better - do some evaluation:
Edit. In 2.10.0-RC1 some methods of
ToolBox
have been renamed.parseExpr
is now justparse
, andrunExpr
is now calledeval
.The most complicated thing here is the raw tree representation of an expression. When one wants to use macros, the macros have to be defined the same way as shown by
showRaw
. But with some helper methods it is possible to define some not so ugly looking macro implementations:But now we come in problems with path-dependent-types - we have to write their paths explicitly if we want not import them.
There is improve of new Scala version