Skip to content

Explain type bounds #53

@davegurnell

Description

@davegurnell

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...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions