清单与类清单。这个 Scala 错误是什么意思?
这个错误是什么意思?
scala> val a = Array[{ def x: Int }](new { def x = 3 })
<console>:5: error: type mismatch;
found : scala.reflect.Manifest[java.lang.Object]
required: scala.reflect.ClassManifest[AnyRef{def x: Int}]
val a = Array[{ def x: Int }](new { def x = 3 })
^
我不知道...
What does this error mean?
scala> val a = Array[{ def x: Int }](new { def x = 3 })
<console>:5: error: type mismatch;
found : scala.reflect.Manifest[java.lang.Object]
required: scala.reflect.ClassManifest[AnyRef{def x: Int}]
val a = Array[{ def x: Int }](new { def x = 3 })
^
I don't have a clue ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,让我们考虑几件事。第一:
这种类型被称为“结构类型”。它定义的不是一个类,而是一组共享具有特定类型签名的方法的对象。在运行时,它被擦除为
Object
,并且对x
的任何调用都是通过反射完成的,因为 Java 没有任何与它等效的东西。接下来:
请注意,您没有使用
new Array
,而是使用Array
。这是对 Scala 的Array
对象的apply
方法的调用。此方法需要一个 ClassManifest 隐式参数来告诉 Scala 如何创建数组。这是必要的,因为在 Java 中数组不会被删除,因此 Scala 必须向 Java 提供精确的类型。问题是:Java 中没有这样的类型。
我确实想知道 Scala 是否不可能在这里使用
Object
。票可能是有序的,但不要指望它是可能的。Ok, let's consider a couple of things. First:
This type is known as a structural type. It defines not a class, but a set of objects that share methods with a certain type signature. At run-time, it is erased to
Object
, and any calls tox
are done through reflection, since Java doesn't have any equivalent to it.Next:
Note that you did not use
new Array
, butArray
. That is a call to theapply
method of Scala'sArray
object. This method requires aClassManifest
implicit parameter that will tell Scala how to create the array. This is necessary because arrays are not erased in Java, so Scala has to provide the precise type to Java.And here's the problem: there's no such type in Java.
I do wonder if it wouldn't be possible for Scala to use
Object
here. A ticket might be in order, but don't count on it being possible.