将新的 Iterable{} 代码从 Scala 2.7.7 移植到 2.8
我看到了这个帖子:
什么Scala 2.8 和 Scala 2.7 之间最大的区别是什么?
它似乎涵盖了一些更改,但我遇到的第一个编译问题似乎没有被提及。有什么建议吗?
- 类型参数的种类(Iterable[Any] with (A with Int) => Any)不符合类 GenericCompanion 中预期的类型参数种类(CC 类型)。 Iterable[Any] with (A with Int) =>; Any 的类型参数与 CC 类型的预期参数不匹配:没有类型参数,但 CC 类型无法
- 创建一个对象,因为 特征 IterableLike 中的方法迭代器 类型 =>迭代器[java.io.File]是 未定义的
- 对象创建是不可能的,因为 特征 IterableLike 中的方法迭代器 类型 => Iterator[V] 未定义
- 重写特征中的方法元素 IterableLike 类型 => 迭代器[java.io.File];方法 元素需要“override”修饰符
- 来覆盖特征中的方法元素 IterableLike 类型 =>迭代器[V]; 方法元素需要“覆盖” 修饰符
这是有问题的代码:
/**
* Filesystem walker.
* <p>
* Less magic version of: http://rosettacode.org/wiki/Walk_Directory_Tree#Scala
*/
object FsWalker {
/**
* Recursive iterator over all files (and directories) in given directory.
*/
def walk(f: File): Iterable[File] = new Iterable[File] {
def elements = {
if (f.isDirectory()) {
// recurse on our child files
f.listFiles.elements.flatMap(child => FsWalker.walk(child).elements)
} else {
// just return given file wrapped in Iterator
Seq(f).elements
}
}
}
}
I saw this thread:
What are the biggest differences between Scala 2.8 and Scala 2.7?
It seems to cover some changes, but the first compile problems I've hit don't seem to be mentioned. Any suggestions?
- kinds of the type arguments (Iterable[Any] with (A with Int) => Any) do not conform to the expected kinds of the type parameters (type CC) in class GenericCompanion. Iterable[Any] with (A with Int) => Any's type parameters do not match type CC's expected parameters: has no type parameters, but type CC has one
- object creation impossible, since
method iterator in trait IterableLike
of type => Iterator[java.io.File] is
not defined - object creation impossible, since
method iterator in trait IterableLike
of type => Iterator[V] is not defined - overriding method elements in trait
IterableLike of type =>
Iterator[java.io.File]; method
elements needs `override' modifier - overriding method elements in trait
IterableLike of type => Iterator[V];
method elements needs `override'
modifier
Here's the code in question:
/**
* Filesystem walker.
* <p>
* Less magic version of: http://rosettacode.org/wiki/Walk_Directory_Tree#Scala
*/
object FsWalker {
/**
* Recursive iterator over all files (and directories) in given directory.
*/
def walk(f: File): Iterable[File] = new Iterable[File] {
def elements = {
if (f.isDirectory()) {
// recurse on our child files
f.listFiles.elements.flatMap(child => FsWalker.walk(child).elements)
} else {
// just return given file wrapped in Iterator
Seq(f).elements
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以前的
元素
现在是迭代器
。您应该使用 -Xmigration 进行编译,以获得有关如何将代码从 2.7 移植到 2.8 的有用提示。
The former
elements
is nowiterator
.You should compile with -Xmigration for helpful hints on how to port your code from 2.7 to 2.8.
这里的关键是确保将方法迭代器与 Iterable 一起使用。 Scala 2.8.0 还做了很多工作来确保类型在集合调用之间保持一致。
我还会考虑使用 Stream 而不是迭代器。迭代器方法在调用迭代器方法时将构造整个文件集。流可能是懒惰的。
或者,如果您想在流中包含目录,请尝试以下操作:
The key here is to make sure you use the method iterator with an Iterable. Scala 2.8.0 also did a lot to ensure that types are consistent across collection calls.
I would also consider using a Stream instead of an iterator. The iterator approach will construct the entire set of files when calling the iterator method. A stream could be lazy.
Alternatively, if you want to include directories in the stream, try the following: