为什么这个简单的 scala 示例无法编译?

发布于 2024-10-20 18:46:10 字数 585 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅人 2024-10-27 18:46:10

我可以建议您尝试不同的方法吗?

扩展集合库是您可以在 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

好久不见√ 2024-10-27 18:46:10

尝试使用 new X() 而不是 X() ——您会收到另一个错误,但我相信它会让您走上正确的轨道。

在后一种情况下,它尝试调用由 X 表示的表达式(例如,想象一下 X 被定义为 object Xval X),并且尝试调用class X的构造函数

。快乐编码。

Try new X() instead of X() -- 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 where X is defined as object X or val X) and not trying to invoke the constructor for class X.

Happy coding.

雪化雨蝶 2024-10-27 18:46:10

目前尚不清楚您要做什么,但它无法编译的原因是没有名为“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文