Я пытаюсь определить метод toList
внутри черты следующим образом:
sealed trait Stream[+A] {
def toList: List[A] = this match {
case Empty => List()
case Cons(h, t) => h()::t().toList()
}
}
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
object Stream {
def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
lazy val head = hd
lazy val tail = tl
Cons(() => head, () => tail)
}
def empty[A]: Stream[A] = Empty
def apply[A](as: A*): Stream[A] =
if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*))
}
Я получаю следующую ошибку компиляции:
stream.scala:16: error: pattern type is incompatible with expected type;
found : Empty.type
required: Stream[A]
case Empty => List()
^
stream.scala:17: error: constructor cannot be instantiated to expected type;
found : Cons[A(in class Cons)]
required: Stream[A(in trait Stream)]
case Cons(h, t) => h()::t().toList()
^
Может кто-нибудь посоветовать?