Covariance in Scala

August 14, 2014

Exercise

Starting with the Option implementation from the previous exercise:

sealed trait Option[A] { ... }
case class Some[A](a: A) extends Option[A]
case class None[A]() extends Option[A]

Make Option covariant in its type A so that only a single None instance is needed:

case object None extends Option[???]
def quotient(divisor: Int)(dividend: Int): Int =
  dividend / divisor

def remainder(divisor: Int)(dividend: Int): Option[Int] =
  (dividend % divisor) match {
    case 0 => None
    case r => Some(r)
  }

val x3 =
  for {
    x <- Some(42)
    q  = quotient(2)(x)
    r <- remainder(4)(x)
  } yield (x,q,r)

println(x3) // Some((42,21,2))