1

以下示例失败,因为Stuff找不到 的定义:

package com.example

import javax.script.ScriptEngineManager

object Driver5 extends App {
  case class Stuff(s: String, d: Double)
  val e = new ScriptEngineManager().getEngineByName("scala")
  println(e.eval("""import Driver5.Stuff; Stuff("Hello", 3.14)"""))
}

我找不到任何允许我在语句中使用自己的类的导入eval语句。难道我做错了什么?如何在 期间使用一个导入类eval

编辑:澄清示例代码以引出更直接的答案。

4

1 回答 1

0

The Scripting engine does not know the context. It surely can't access all the local variables and imports in the script, since they are not available in the classfiles. (Well, variable names may be optionally available as a debug information, but it is virtually impossible to use them for this purpose.)

I am not sure if there is a special API for that. Imports are different across various languages, so bringing an API that should fit them all can be difficult.

You should be able to add the imports to the eval-ed String. I am not sure if there is a better way to do this.

于 2014-12-08T18:50:58.283 回答