扩展 Scala 集合
我想派生一个 Scala 内置集合的版本,该版本扩展了特定泛型类型的功能,例如,
import scala.collection.immutable._
class Tuple2Set[T1,T2] extends HashSet[Tuple2[T1,T2]] {
def left = map ( _._1 )
def right = map ( _._2 )
}
但是当我尝试将它用于以下测试时,
new Tuple2Set[String,String]() + (("x","y")) left
我收到以下编译错误
error: value left is not a member of scala.collection.immutable.HashSet[(String, String)]
如何更改该类这有效吗?
I would like to derive a version of a Scala built-in collection that expands on the functionality for a particular generic type e.g.,
import scala.collection.immutable._
class Tuple2Set[T1,T2] extends HashSet[Tuple2[T1,T2]] {
def left = map ( _._1 )
def right = map ( _._2 )
}
However when I try to use it with the following test
new Tuple2Set[String,String]() + (("x","y")) left
I get the following compile error
error: value left is not a member of scala.collection.immutable.HashSet[(String, String)]
How can I change the class so that this works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您确定确实需要扩展 Scala 集合吗?要使上面的代码工作,您可以这样做:
在这种情况下,Tuple2Set 只是任何其他 Set 实现的包装器。这意味着您不再局限于
HashSet
,并且您的方法left
和right
也可以在任何其他实现(如 TreeSet)上使用。我认为在大多数情况下,包装或组合+委托比继承效果更好(并且导致的问题更少)。
Are you sure you really need to extend Scala collection? To make the code above work you can do this:
In this case
Tuple2Set
is just the wrapper for any otherSet
implementations. This means you are not limited toHashSet
anymore and your methodsleft
andright
will be available on any other implementations as well (like TreeSet).I think in most cases wrapping or composition+delegation works much better than inheritance (and causes less problems).
正如Kevin Wright所说,
+
操作将返回HashSet
。类型类CanBuildFrom
用于在map
等操作期间构建新集合。因此,如果您希望+
返回Tuple2Set
而不是HashSet
,您应该实现CanBuildFrom
并使其在同伴中隐式可用像这样的对象:As Kevin Wright said,
+
operation will return backHashSet
. Type classCanBuildFrom
is used to build new collections during operations likemap
. So if you want+
to returnTuple2Set
instead ofHashSet
you should implementCanBuildFrom
and make it implicitly available in companion object like this:您的问题的一般答案对于此处的回复来说有点过于复杂。但它已经写在一些 网页。
具有更多上下文的相同材料也包含在我们的书《Scala 编程》(Artima Press)的第二版中。
The general answer to your question is a bit too involved for a response here. But it has been written up in some web pages.
The same material with more context is also in the 2nd edition of our book, Programming in Scala, Artima Press.
您的示例不起作用的原因是
+
操作的返回类型是HashSet
而不是Tuple2Set
。使用“为我的图书馆拉皮条”模式而不是继承,你会更幸运。
The reason your example doesn't work is that the return type of the
+
operation is aHashSet
and not aTuple2Set
.You'll have much more luck using the "pimp my library" pattern instead of inheritance.