underscoreio/essential-scala

Explain type bounds

Opened this issue · 0 comments

The recently updated getOrElse example at the top of options.md uses upper bounds. We need to describe them!

sealed trait Option[+A] {
  def getOrElse[B >: A](default: B): B

  def isEmpty: Boolean
  def isDefined: Boolean = !isEmpty

  // other methods...
}

final case class Some[A](x: A) extends Option[A] {
  def getOrElse[B >: A](default: B) = x

  def isEmpty: Boolean = false

  // other methods...
}

case object None extends Option[Nothing] {
  def getOrElse[B >: Nothing](default: B) = default

  def isEmpty: Boolean = true

  // other methods...
}