Skip to content

Instantly share code, notes, and snippets.

@wangjingke
Forked from xuwei-k/Eval.scala
Created April 3, 2018 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangjingke/5cceb750fbd5781c066b3da0a048fa0d to your computer and use it in GitHub Desktop.
Save wangjingke/5cceb750fbd5781c066b3da0a048fa0d to your computer and use it in GitHub Desktop.
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment