I am working my way through the "Functional Programming in Scala" book. Chapter 5.2 starts with the following code:
sealed trait Stream[+A]
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: _*))
}
Then the following definition is given:
def headOption: Option[A] = this match {
case Empty => None
case Cons(h, t) => Some(h())
}
I assume it should be part of the companion object. To not get a compile error immediately I add "headOption[A]". But when I include the result into "object Stream" I am getting the error "pattern type is incompatible with expected type; found : myPackage.Empty.type required: myPackage.Stream.type" with "Empty" underlined.
I am a little lost here. What am I doing wrong?