Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active June 1, 2023 06:41
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xuwei-k/9ba39fe22f120cb098f4 to your computer and use it in GitHub Desktop.
Save xuwei-k/9ba39fe22f120cb098f4 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))
}
@marxangels
Copy link

how to pass context variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment