scala collection.Map 无法添加到

发布于 2024-07-22 17:44:20 字数 297 浏览 8 评论 0原文

为什么我无法添加到 scala.collection.Map? 如果没有这个功能,这个trait似乎毫无用处。

难道他们不能重写 Iterable 中的 ++ 方法并将返回类型减少为 Map 吗?

PS我并不是说它应该是可变的,只是它应该能够返回一个带有添加的映射(或多个映射)的新Map,与immutable.Map

Why can't I add to a scala.collection.Map? It seems that this trait is pretty useless without this functionality.

Couldn't they have overridden the ++ method in Iterable and reduced the return type to a Map?

P.S. I don't mean that it should be mutable, just that it should be able to return a new Map with an added mapping (or mappings), the same as an immutable.Map.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寄意 2024-07-29 17:44:21

我将在下面留下原始答案,尽管它非常不正确,因为我没有正确理解这个问题。

Scala 目前的集合库几乎强制执行可变/不可变的不同添加方法,可能是希望在源代码中明确正在使用什么类型的集合。 如前所述,这在 2.8 中进行了修订,并且该前提将消失。

既然如此,抽象类不提供您正在考虑的方法,因为它们可能存在用于不可变但不用于可变,反之亦然。 或者它们可能具有相同的名称,但实现不同。

因此,不可能在基类中提供它们。

但是,此外,请注意,这样,如果您收到 scala.collection.map,您不能在收到不可变数据时将其视为可变数据,从而搞砸了,反之亦然。

现在,错误的答案。 :)

你可以(不,你不能——下面的代码使用 scala.collection.imutable.Map)。

scala> val x = Map(1 -> 'a', 2 -> 'b')
x: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b)

scala> x ++ Map(3 -> 'c')
res5: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b, 3 -> c)

scala> var y = x
y: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b)

scala> y += (3 -> 'c')

scala> y
res7: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b, 3 -> c)

scala> x + (3 -> 'c')
res8: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b, 3 -> c)

I'll leave the original answer below, though it is pretty much incorrect, as I didn't understand the question correctly.

Scala's present collection library pretty much enforces different adding methods for mutable/immutable, probably in the hope of making clear in the source code what type of collection is being used. As said, this is being revised in 2.8, and that premise is going away.

That being the case, the abstract classes do not provide the methods you are thinking of, because they might exist for immutable but not for mutable, and vice versa. Or they might have the same name, but different implementations.

And, therefore, it would be impossible to provide them in the base class.

But, furthermore, notice that, this way, if you receive a scala.collection.map, you cannot screw it up by treating it as mutable when you received an immutable, or vice versa.

And, now, for the wrong answer. :)

You can (no, you can't -- the code below uses scala.collection.imutable.Map).

scala> val x = Map(1 -> 'a', 2 -> 'b')
x: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b)

scala> x ++ Map(3 -> 'c')
res5: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b, 3 -> c)

scala> var y = x
y: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b)

scala> y += (3 -> 'c')

scala> y
res7: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b, 3 -> c)

scala> x + (3 -> 'c')
res8: scala.collection.immutable.Map[Int,Char] = Map(1 -> a, 2 -> b, 3 -> c)
倾其所爱 2024-07-29 17:44:21

Scala 集合库目前存在相当大的缺陷。 2.8(预计将在一个月左右发布)有一个完全修改的集合库,我相信它具有您正在寻找的行为。

The Scala collections library is currently fairly flawed. 2.8 (due for release in a month or so) has a completely revamped collections library that I believe has the behavior you're looking for.

仙气飘飘 2024-07-29 17:44:21

我的猜测是:
如果您有一些集合并需要为其他人提供只读 Map 外观,则此特征适合该工作。 如果您在那里添加附加内容,那么它就不是正交设计,因为那时需要只读地图界面。 另一方面,如果需要通用接口中的 ++ 操作,则它不适用于可变和不可变实现。 例如,如果可变集合在添加时返回 self,那么仅通过查看接口就不会明显看出发生了什么。

My guess is:
If you have some collection and need to provide a read only Map facade for others, this trait is suitable for the job as is. If you include addition there it would not be orthogonal design since one would need then read-only map interface then. On the other hand if ++ operation in generic interface is required it would not be suitable for both mutable and immutable implementations. E.g. if mutable collection returned self on addition it would not be obvious what happens just by looking at interface.

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