Vishnu Prasad bio photo

Vishnu Prasad

Just another boring time Travelling programmer from the streets of Gotham.

Email Twitter Facebook Google+ LinkedIn Instagram Github Stackoverflow Steam Flickr Gitter

Twitter’s Eval can be used to Dynamically compile and use the Code in your application thus making it very effective for Dynamic Programming.

Maven Dependency



The Eval Function evaluates files, strings, or input streams as Scala code, and returns the result.

val eval = new Eval(target : scala.Option[])

The eval function takes a single parameter Target. If the target is None, the results are compiled to memory (and are therefore ephemeral). If target is Some(path), the path must point to a directory, and classes will be saved into that directory.

The flow of evaluation is:
  • extract a string of code from the file, string, or input stream
  • run preprocessors on that string
  • wrap processed code in an apply method in a generated class
  • compile the class
  • contruct an instance of that class
  • return the result of apply()

Declare an Interface which your Dynamic Code should Follow

For the Example I’ve declared a variable name and a function orderString in my trait.

package com.example

trait EvalInterface extends Serializable {
  val name:String
  def orderString(i: String): String

The Dynamic Code which would be evaluated at runtime

Saving the file in the location /path/tsv.scala

package com.example.EvalInterface
new EvalInterface {
  override val name:String="TSV"
  override def orderString(i: String): String = {

Saving the file in the location /path/csv.scala

package com.example.EvalInterface
new EvalInterface {
  override val name:String="CSV"
  override def orderString(i: String): String = {

Now we can Load these files at runtime to execute the functions

The Dynamic Code Evaluation

package com.example

import com.twitter.util.Eval

object EvalExample extends App {

val eval = new Eval // Initializing The Eval without any target location

val csvEval: EvalInterface = eval[EvalInterface](new File("/path/csv.scala"))

val tsvEval: EvalInterface = eval[EvalInterface](new File("/path/tsv.scala"))

val records = Array(

records.foreach( i => println(csvEval.orderString( i )))
records.foreach( i => println(tsvEval.orderString( i )))