如何在 Scala 2.9.0 中重写 Iterable.flatMap?
我刚刚将一些代码更新到 2.9.0,但遇到了问题。我有一个称为“NonStrictIterable”的特征(本质上,一切都应该尽可能懒惰——NonStrictIterable 本身之外的任何代码都不应运行,直到有人真正请求一个元素)。
然而,在 2.9.0 中,我似乎无法覆盖 flatMap。这是一个精简版本,显示了错误:
import scala.collection.generic.CanBuildFrom
trait NonStrictIterable[A] extends Iterable[A] { self =>
def iterator: Iterator[A]
override def flatMap[B, That](f: A => TraversableOnce[B])(implicit bf: CanBuildFrom[Iterable[A], B, That]): That = {
new NonStrictIterable[B] {
def iterator = self.iterator flatMap { a: A => f(a).toIterable.iterator }
}.asInstanceOf[That]
}
}
这曾经在 2.9.0 之前工作,但现在我得到“方法 flatMap 不覆盖任何内容”。查找 Iterable.flatMap 的方法签名,我发现类型签名中的 TraversableOnce
已更改为 enTraversableOnce
。进行相应的更改后,
import scala.collection.GenTraversableOnce
import scala.collection.generic.CanBuildFrom
trait NonStrictIterable[A] extends Iterable[A] { self =>
def iterator: Iterator[A]
override def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Iterable[A], B, That]): That = {
new NonStrictIterable[B] {
def iterator = self.iterator flatMap { a: A => f(a).toIterable.iterator }
}.asInstanceOf[That]
}
}
我收到编译器错误“错误或无法访问的类型”。
发生什么事了?有人可以提出解决方法吗?
I'm just updating some of my code to 2.9.0, and I've run into a problem. I have a trait that I call "NonStrictIterable" (essentially, everything should be as lazy as possible --- no code outside NonStrictIterable itself should run until someone actually asks for an element).
In 2.9.0, I don't seem to be able to override flatMap, however. Here's a cut down version, that exhibits the error:
import scala.collection.generic.CanBuildFrom
trait NonStrictIterable[A] extends Iterable[A] { self =>
def iterator: Iterator[A]
override def flatMap[B, That](f: A => TraversableOnce[B])(implicit bf: CanBuildFrom[Iterable[A], B, That]): That = {
new NonStrictIterable[B] {
def iterator = self.iterator flatMap { a: A => f(a).toIterable.iterator }
}.asInstanceOf[That]
}
}
This used to work pre 2.9.0, but now I get "method flatMap overrides nothing". Looking up the method signature for Iterable.flatMap, I see that TraversableOnce
in the type signature has changed to enTraversableOnce
. Making the corresponding change
import scala.collection.GenTraversableOnce
import scala.collection.generic.CanBuildFrom
trait NonStrictIterable[A] extends Iterable[A] { self =>
def iterator: Iterator[A]
override def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Iterable[A], B, That]): That = {
new NonStrictIterable[B] {
def iterator = self.iterator flatMap { a: A => f(a).toIterable.iterator }
}.asInstanceOf[That]
}
}
I get the compiler error "erroneous or inaccessible type".
What's going on? Can someone suggest a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要升级到2.9 Final。这是之前 RC 中的一个错误,现已修复。
You need to upgrade to 2.9 final. This was an error in a previous RC which has been fixed.
对我有用。我怀疑您实际上是针对 RC3 中的 scala-library.jar 进行编译。
Works for me. I suspect that you are actually compiling against scala-library.jar from RC3.