Using twitter Eval library : https://twitter.github.io/util/docs/index.html#com.twitter.util.Eval
val i: Int = new com.twitter.util.Eval()("1 + 1")
println(i)
prints 2
val fun = new com.twitter.util.Eval()("object obj { def fun = {println(\"fun\")}; fun() }")
println(i)
causes exception :
Exception in thread "main" com.twitter.util.Eval$CompilerException: Compiler exception error: line 1: Unit does not take parameters
object obj { def fun = {println("fun")}; fun() }
^
at com.twitter.util.Eval$StringCompiler.apply(Eval.scala:585)
at com.twitter.util.Eval$StringCompiler$$anonfun$apply$2.apply(Eval.scala:596)
at com.twitter.util.Eval$StringCompiler$$anonfun$apply$2.apply(Eval.scala:595)
at scala.Option.getOrElse(Option.scala:121)
at com.twitter.util.Eval$StringCompiler.apply(Eval.scala:595)
at com.twitter.util.Eval.applyProcessed(Eval.scala:203)
at com.twitter.util.Eval.applyProcessed(Eval.scala:196)
at com.twitter.util.Eval.apply(Eval.scala:142)
at gen.Compiler$.delayedEndpoint$gen$Compiler$1(Compiler.scala:8)
at gen.Compiler$delayedInit$body.apply(Compiler.scala:4)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at gen.Compiler$.main(Compiler.scala:4)
at gen.Compiler.main(Compiler.scala)
Can twitter Eval library be used to invoke function evaluated by Eval library ?
Eval version :
<dependency>
<groupId>com.twitter</groupId>
<artifactId>util-eval_2.11</artifactId>
<version>6.30.0</version>
</dependency>