Created
September 10, 2013 19:15
-
-
Save dcsobral/6514200 to your computer and use it in GitHub Desktop.
Prints a string as if it was produced by REPL. However, it has none of the magic the REPL has for printing types like arrays, or using quotes for empty strings on collections, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Printer { | |
import scala.reflect.runtime.currentMirror | |
import scala.tools.reflect.ToolBox | |
val toolBox = currentMirror.mkToolBox() | |
def print(s: String) = { | |
val tree = toolBox parse s | |
val typedTree = toolBox typeCheck tree | |
val tpe = typedTree.tpe.widen | |
val result = toolBox eval tree | |
s"""|scala> $s | |
|$tpe: $result | |
|""".stripMargin | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment