【发布时间】:2017-08-09 08:55:18
【问题描述】:
我很难理解 Scala 的基础知识,尤其是 this 代码:
trait RichIterator extends AbsIterator {
def foreach(f: T => Unit): Unit = while (hasNext) f(next())
}
对我来说,foreach 看起来像一个返回单位的方法。它接受一个函数,f作为参数:
f: T => Unit
它也返回单位。该方法在 hasNext 为 true 时循环,我不知道 f(next()) 做了什么。这是 f 输入参数吗?为什么要使用似乎只返回一个单位的函数?有人可以帮我解释一下吗。
【问题讨论】:
标签: scala