I'm working through Functional Programming in Scala (first edition) and coming across an error when working through a solution to the first exercise in Chapter 4: Handling errors without exceptions.
The chapter rebuilds the Option type using the following structure:
sealed trait Option[+A]
case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]
The first exercise instructs the reader to implement the following methods on the Option type:
trait Option[+A] {
def map[B](f: A => B): Option[B]
def flatMap[B](f: A => Option[B]): Option[B]
def getOrElse[B >: A](default: => B): B
def orElse[B >: A](ob: => Option[B]): Option[B]
def filter(f: A => Boolean): Option[A]
}
When loading the solution verified by the answer key for the book via the scala REPL
sealed trait Option[+A] {
def map[B](f: A => B): Option[B] = this match {
case Some(a) => Some(f(a))
case None => None
}
}
case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]
I receive a compilation error:
scala> :load ErrorHandling.scala
val args: Array[String] = Array()
Loading ErrorHandling.scala...
case Some(a) => Some(f(a))
^
ErrorHandling.scala:3: error: constructor cannot be instantiated to expected type;
found : Some[A(in class Some)]
required: Option[A(in trait Option)]
case Some(a) => Some(f(a))
^
ErrorHandling.scala:3: error: type mismatch;
found : Some[B]
required: Option[B]
case None => None
^
ErrorHandling.scala:4: error: pattern type is incompatible with expected type;
found : None.type
required: Option[A]
case None => None
^
ErrorHandling.scala:4: error: type mismatch;
found : None.type
required: Option[B]
Given None and Some extend Option, I'm not sure where the type mismatch is arising.
Any help would be greatly appreciated!