jweinst1
6/7/2014 - 8:07 AM

Eval.scala

libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value

scalaVersion := "2.11.8"

name := "eval"
package eval

import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
import java.io.File

object Eval {

  def apply[A](string: String): A = {
    val toolbox = currentMirror.mkToolBox()
    val tree = toolbox.parse(string)
    toolbox.eval(tree).asInstanceOf[A]
  }

  def fromFile[A](file: File): A =
    apply(scala.io.Source.fromFile(file).mkString(""))

  def fromFileName[A](file: String): A =
    fromFile(new File(file))

}