-
-
Save wangjingke/5cceb750fbd5781c066b3da0a048fa0d to your computer and use it in GitHub Desktop.
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
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value | |
scalaVersion := "2.11.8" | |
name := "eval" |
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
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