Skip to content

Instantly share code, notes, and snippets.

@stew
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save stew/311861e5099a4d8f10da to your computer and use it in GitHub Desktop.

Select an option

Save stew/311861e5099a4d8f10da to your computer and use it in GitHub Desktop.
package maybe
sealed trait Maybe[A] {
def isEmpty: Boolean
}
case class Just[A](a: A) extends Maybe[A] {
override final def isEmpty = true
}
case object Empty extends Maybe[Nothing] {
override final val isEmpty = false
def unapply[A](m: Maybe[A]) = m.isEmpty
def apply[A](): Maybe[A] = this.asInstanceOf[Maybe[A]] // YOLO
}
object Main extends App {
val x: Maybe[Int] = Just(1)
x match {
case Just(_) ⇒ 1
}
val y: Option[Int] = Some(1)
y match {
case Some(_) ⇒ 1
}
}
// compilation run
// > scalac maybe.scala
// maybe.scala:27: warning: match may not be exhaustive.
// It would fail on the following input: None
// y match {
// ^
// one warning found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment