Scala“fromFile”奇怪吗?
我不明白为什么两段代码本应做完全相同的事情,但在 Scala 中却做了不同的事情。
第一个例子:
scala> val ggg = Source.fromFile("/somefile");
ggg: scala.io.BufferedSource = non-empty iterator
scala> ggg.getLines();
res67: Iterator[String] = empty iterator
第二个例子:
scala> Source.fromFile("/somefile").getLines();
res68: Iterator[String] = non-empty iterator
他们不是想做同样的事情吗,还是我错过了什么?
I can't understand why two bits of code that are meant to do exactly the same thing, do different things in Scala.
First example:
scala> val ggg = Source.fromFile("/somefile");
ggg: scala.io.BufferedSource = non-empty iterator
scala> ggg.getLines();
res67: Iterator[String] = empty iterator
Second example:
scala> Source.fromFile("/somefile").getLines();
res68: Iterator[String] = non-empty iterator
Aren't they meant to do the same thing, or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是 BufferedSource.toString 的一个怪癖(bug?)。观察:
为了显示表达式的值,REPL 需要调用 BufferedSource.toString,这会产生清空迭代器的副作用。
This seems to be a quirk (bug?) with
BufferedSource.toString
. Observe:To show the value of the expression, the REPL needs to call
BufferedSource.toString
, and this has the side effect of emptying the iterator.看起来像这个错误:SI-4662。
显然已在主干 变更集 25212 中修复,但在 2.9 中未修复.1 据我所知。
在错误注释中提到它可能只在 REPL 中体现,而不是在“真实”代码中体现。
Looks like this bug: SI-4662.
Apparently fixed in trunk Changeset 25212, but not in 2.9.1 as far as I can see.
In the bug notes it's mentioned that it probably manifests itself only in the REPL, not in "real" code.