如何在 Scala 2.9.0 中重写 Iterable.flatMap?

发布于 2024-11-07 01:55:48 字数 1328 浏览 1 评论 0原文

我刚刚将一些代码更新到 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 技术交流群。

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

发布评论

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

评论(2

万水千山粽是情ミ 2024-11-14 01:55:48

您需要升级到2.9 Final。这是之前 RC 中的一个错误,现已修复。

You need to upgrade to 2.9 final. This was an error in a previous RC which has been fixed.

蓝梦月影 2024-11-14 01:55:48

对我有用。我怀疑您实际上是针对 RC3 中的 scala-library.jar 进行编译。

 ~/code/scalaz[scala-2.9.x*]: scala29
Welcome to Scala version 2.9.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.GenTraversableOnce
import scala.collection.GenTraversableOnce

scala> import scala.collection.generic.CanBuildFrom
import scala.collection.generic.CanBuildFrom

scala> 

scala> 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]
     |  }
     | }
defined trait NonStrictIterable

Works for me. I suspect that you are actually compiling against scala-library.jar from RC3.

 ~/code/scalaz[scala-2.9.x*]: scala29
Welcome to Scala version 2.9.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.GenTraversableOnce
import scala.collection.GenTraversableOnce

scala> import scala.collection.generic.CanBuildFrom
import scala.collection.generic.CanBuildFrom

scala> 

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