Can twitter eval library be used to invoke evaluat

2019-08-11 08:02发布

问题:

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>

回答1:

It looks like that you can, but I only succeeded to do it outside the scope of the object. It might or might fit your needs:

val fun = new com.twitter.util.Eval().apply[Unit]("""
  object obj { 
    def fun() = {
      println("fun")
    }
  } 

  obj.fun()
  """
)

You also have to specify the type of the evaluated expression, so in order to do that you have to call explicitly the apply function, which takes a type parameter.

update: Instead of Unit, you could return a Function0[Unit] from the eval block, and assign it to the val fun:

scala> val fun = new com.twitter.util.Eval().apply[Function0[Unit]]("""
    object obj {
      def fun() = {
        println("printing fun")
      }
    }
    obj.fun()
    obj.fun _
  """
)

printing fun
fun: () => Unit = <function0>

scala> fun()
printing fun


标签: scala twitter