Last active
August 29, 2015 14:04
-
-
Save stew/311861e5099a4d8f10da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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