Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"eval" in Scala

People also ask

What is eval in Scala?

Eval is a data type for controlling synchronous evaluation. Its implementation is designed to provide stack-safety at all times using a technique called trampolining.

What is an eval statement?

The eval statement is used to evaluate the value of a variable or a column within a record, and if necessary, convert it into another data type. In stitchers, eval statements are used in combination with OQL to extract values from records in one database and insert those values into another database.

What is eval in coding?

In some programming languages, eval , short for the English evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval .

What does eval do in Lisp?

eval provides direct access to the LISP expression evaluator. It causes the evaluation of whatever <exp> returns. Thus, if <exp> is an evaluable LISP expression and itself returns and evaluable LISP expression, then eval returns the value of the second evaluation.


it's now 2011, and you can do so with scala.tools.nsc.Interpreter

see http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/


Scala is not a scripting language. It may look somewhat like a scripting language, and people may advocate it for that purpose, but it doesn't really fit well within the JSR 223 scripting framework (which is oriented toward dynamically typed languages). To answer your original question, Scala does not have an eval function just like Java does not have an eval. Such a function wouldn't really make sense for either of these languages given their intrinsically static nature.

My advice: rethink your code so that you don't need eval (you rarely do, even in languages which have it, like Ruby). Alternatively, maybe you don't want to be using Scala at all for this part of your application. If you really need eval, try using JRuby. JRuby, Scala and Java mesh very nicely together. It's quite easy to have part of your system in Java, part in Scala and another part (the bit which requires eval) in Ruby.


Scala has added official support to JSR-223 in 2.11 (https://issues.scala-lang.org/browse/SI-874).

So if you still need it after thinking about the considerations made in the currently accepted answer from Daniel Spiewak (about rethinking in a way it is not needed), this should be the official alternative.


You can emulate "eval" by taking scala code, wrapping it in a class, compiling that class, using reflection to create a new instance, and then calling it. It's a little involved, and the scala compiler is very slow (on the order of 2 seconds) to initialize, but it works fine.

There's a library for it here, called "util-eval": https://github.com/twitter/util/

The code in question lives here: https://github.com/twitter/util/blob/master/util-eval/src/main/scala/com/twitter/util/Eval.scala

It works like this:

val sum = Eval[Int]("1 + 1")
// sum will be 2