Skip to content

Instantly share code, notes, and snippets.

@dcsobral
Created September 10, 2013 19:15
Show Gist options
  • Save dcsobral/6514200 to your computer and use it in GitHub Desktop.
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.
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