您好我正在阅读一本名为" Scala中的函数式编程"的书。书上有这样的代码
trait Stream[+A] {
def uncons: Option[(A, Stream[A])]
def isEmpty: Boolean = uncons.isEmpty
}
object Stream {
...
def cons[A](hd: => A, tl: => Stream[A]): Stream[A] =
new Stream[A] {
lazy val uncons = Some((hd, tl))
}
...
它说hd: => A
语法使这个懒惰的val
的内容
trait Stream[+A]{
def toList: List[A] = {
@annotation.tailrec
def go(s: Stream[A], acc: List[A]) : List[A] = s match {
case Cons(h,t) => go(t(), h() :: acc)
case _ => acc
}
go(this, List()).reverse
}
}
case class Cons[+A](h: ()=>A, t: () => Stream[A]) extends Stream[A]
h: =>A
和h: ()=>A
之间有什么区别。()
go(t(), h(), :: acc)
醇>