scala:FOO不能转换为FOO [英] scala: FOO cannot be cast to FOO

查看:259
本文介绍了scala:FOO不能转换为FOO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala,我非常困惑的这个相当重复的错误消息:

In Scala, I am incredibly confused by this fairly tautological error message:

java.lang.ClassCastException: FOO cannot be cast to FOO

我希望有人总是可以转换成自己的类型。

I would expect that someone can always be cast to its own type.

我试图对scala编译器运行下面的包装器,位于 http: //code.google.com/p/rooscaloo/source/browse/trunk/rooscaloo/src/org/darevay/rooscaloo/Interpreter.scala 不幸的是,Scala正在说 ResultHolder无法投射到ResultHolder 当我执行以下操作:

I am trying to run the following wrapper around the scala compiler, located at http://code.google.com/p/rooscaloo/source/browse/trunk/rooscaloo/src/org/darevay/rooscaloo/Interpreter.scala Unfortunately, Scala is saying ResultHolder cannot be cast to ResultHolder when I do the following:

import org.darevay.rooscaloo._
println(new Interpreter().eval("2"))

c> println 应该接受 Any 。应该这样做的是Interpreter.eval返回 ResultHolder 类型,使得 ResultHolder.value 将等于 2 。

I thought println was supposed to accept Any. What should be going on is that Interpreter.eval returns a ResultHolder type, such that ResultHolder.value would be equal to 2.

此外,尝试打印 .value 不工作的错误:

Additionally, trying to print .value doesn't work with the error:

(fragment of Test.scala):3: error: value value is not a member of Any                                                                                                                                                 
println(new Interpreter().eval("2").value)






更多详情




More details

java.lang.reflect.InvocationTargetException                                                                                                                                                                           
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        ...
Caused by: java.lang.ClassCastException: org.darevay.rooscaloo.ResultHolder cannot be cast to org.darevay.rooscaloo.ResultHolder
        at binder0$.set(<script>:1)
        at binder0.set(<script>)
        ... 24 more (unsure how to print them)



< scala.tools.nsc.interpreter 包试图做奇怪的事情。

Though I am a Scala newbie, perhaps the internal mechanism binder of the scala.tools.nsc.interpreter package is trying to do something weird.

我调用脚本为 scala Test.scala

我的问题是:这个错误消息的原因是什么,真正的问题是什么,以及如何获取scala.tools.nsc。[interpreter]的示例代码。感谢。

My question is: What is the cause of this error message, what is the real issue, and how does one get working sample code for scala.tools.nsc.[interpreter]? Thanks.

推荐答案

我遇到类似的问题,我已经转换我的代码使用 IMain#mostRecentVar 在Scala 2.9中引入。以下是 CompilerMatcher 我写道:

I've run into similar issue, and I've converted my code to use IMain#mostRecentVar introduced in Scala 2.9. Here's from CompilerMatcher I wrote:

  val main = new IMain(s)
  main.compileSources(files.map(toSourceFile(_)): _*)
  code map { c => main.interpret(c) match {
    case IR.Error => error("Error interpreting %s" format (c))
    case _ => 
  }}
  val recent = main.mostRecentVar
  val holder = main.valueOfTerm(recent)
  if (holder != Some(expected))
    println("actual: " + holder.map(_.toString).getOrElse{"None"})

这篇关于scala:FOO不能转换为FOO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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