我使用 Scala 2.9.1。我有一个简单的 Scala“解释器”:
import scala.tools.nsc.interpreter.IMain
import scala.tools.nsc.interpreter.Results.Result
import scala.tools.nsc.interpreter.Results.Success
object App {
def main(args: Array[String]) {
val interpreter = new IMain
val result:Result = interpreter.interpret(args(0))
result.toString() match {
case "Success" =>
{
var success = result.asInstanceOf[Success]
println(success.productElement(0))
};
case _ => println("very bad result");
}
}
}
当我尝试编译它(maven)时,我得到:
[ERROR] /home/koziolek/workspace/dsi/src/main/scala/pl/koziolekweb/scala/dsi/App.scala:15: error: not found: type Success
[INFO] var success = result.asInstanceOf[Success]
如您所见,编译器说没有类型 Success,尽管我导入了它。
最佳答案
Success
是一个对象,而不是一个类,您需要将其转换为它的单例类型 result.asInstanceOf[Success.type]
。显然,您正试图解决不知道如何进行模式匹配的问题。这将使您无需强制转换即可获得正确的结果:
import tools.nsc.interpreter.Results._
result match {
case Success => "yes!"
case Error => "no..."
case Incomplete => "you missed something"
}
如果您想在成功的情况下获得解释表达式的结果值,请参阅my reply in this post了解更多详情。
关于scala - 尽管已导入,但未找到解释器结果类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452484/