为什么这个简单的 scala 示例无法编译?
class X extends Map[String, String] {
def x(): X = { X() } // can't be compiled
}
无法编译,错误是:
<console>:6: error: not found: value X
def x(): X = { X() } // can't be compiled
Why is X not found?我看不出如何纠正它。
更新:
我现在知道原因了。我想要做的是创建一个扩展 HashMap
的类,因为 Map()
将返回 HashMap
的实例,所以我想我可以只是扩展Map
。现在,正确的代码应该是:
import scala.collection.immutable.HashMap
class X extends HashMap[String, String] {
def x(): X = { new X() }
}
class X extends Map[String, String] {
def x(): X = { X() } // can't be compiled
}
can't be compiled, the error is:
<console>:6: error: not found: value X
def x(): X = { X() } // can't be compiled
Why is X not found? I can't see how to correct it.
UPDATE:
I know the reason now. What I want to do is create a class which extends HashMap
, since Map()
will return an instance of HashMap
, so I thought I can just extends Map
. Now, the correct code should be:
import scala.collection.immutable.HashMap
class X extends HashMap[String, String] {
def x(): X = { new X() }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以建议您尝试不同的方法吗?
扩展集合库是您可以在 Scala 中执行的更高级的操作之一。要正确完成这项工作,您需要深入了解高级类型、推理、方差、隐式和 CanBuildFrom 机制。对于初学者来说,这并不是一项轻松的任务。
另一方面,您实际上很少需要扩展集合。
回到首要原则...您认为正确的方法是扩展 HashMap 来尝试解决什么问题?我几乎可以保证在 Scala 中有更好的方法来做到这一点。
UPDATE Mk.II
此答案之前包含集合框架
CanBuildFrom
逻辑的描述。按照@soc的建议,我现在已将部分答案迁移到 此处常见问题解答Can I recommend that you try a different approach?
Extending the collection library is one of the more advanced things you can do in Scala. To do the job properly you need a deep understanding of higher-kinded types, inference, variance, implicits, and the
CanBuildFrom
mechanism. This is not a light-hearted task to be taken on by a beginner.On the other hand, it's incredibly rare that you'll ever actually need to extend a collection.
Go back to first principles... What problem are you trying to solve for which you think the correct approach is to extend
HashMap
? I can virtually guarantee that there's a much better way to do it in Scala.UPDATE Mk.II
This answer previously contained a description of the collection framework
CanBuildFrom
logic. Following @soc's suggestion, I've now migrated that part of the answer to an FAQ question here尝试使用
new X()
而不是X()
——您会收到另一个错误,但我相信它会让您走上正确的轨道。在后一种情况下,它尝试调用由
X
表示的表达式(例如,想象一下X
被定义为object X
或val X
),并且不尝试调用class X的构造函数。快乐编码。
Try
new X()
instead ofX()
-- you'll get another error, but it'll set you on the right track I believe.In the latter case it is trying to invoke apply upon the expression denoted by
X
(e.g. imagine whereX
is defined asobject X
orval X
) and not trying to invoke the constructor for class X.Happy coding.
目前尚不清楚您要做什么,但它无法编译的原因是没有名为“X”的字段、函数或方法,并且没有带有“apply()”方法的模块“X”。
“如何纠正?”
明确你想要实现的目标。
It's not clear what you are trying to do, but the reason it doesn't compile is that there is no field, function or method called 'X', and there is no module 'X' with an 'apply()' method.
"How to correct it?"
Clarify what you are trying to achieve.