twitter eval 库可以用于调用评估代码吗? [英] Can twitter eval library be used to invoke evaluated code?

查看:27
本文介绍了twitter eval 库可以用于调用评估代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 twitter Eval 库:https://twitter.github.io/util/docs/index.html#com.twitter.util.Eval

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)

打印 2

val fun = new com.twitter.util.Eval()("object obj { def fun = {println(\"fun\")}; fun() }")
  println(i)

导致异常:

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)

twitter Eval 库可以用来调用 Eval 库求值的函数吗?

Can twitter Eval library be used to invoke function evaluated by Eval library ?

评估版:

<dependency>
    <groupId>com.twitter</groupId>
    <artifactId>util-eval_2.11</artifactId>
    <version>6.30.0</version>
</dependency>

推荐答案

看起来是可以的,但我只是在 object 的范围之外成功了.它可能或可能满足您的需求:

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()
  """
)

您还必须指定计算表达式的类型,因此为了做到这一点,您必须显式调用 apply 函数,该函数接受一个类型参数.

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:代替 Unit,您可以从 eval 块返回一个 Function0[Unit],并将其分配给 val 乐趣:

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

这篇关于twitter eval 库可以用于调用评估代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆