Created
June 26, 2017 11:41
-
-
Save nicktelford/876bd6cb94733f795f8167a74bebb7ce to your computer and use it in GitHub Desktop.
Cheap Options in dotty?
This file contains 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
type Option[A] = A | Null | |
object Option { | |
inline def apply[A](x: A): Option[A] = x | |
} | |
implicit class OptionMethods[A](val x: Option[A]) extends AnyVal { | |
inline def map[B](f: A => B): Option[A] = x match { | |
case null => null | |
case a => f(a) | |
} | |
inline def flatMap[B](f: A => Option[B]): Option[B] = x match { | |
case null => null | |
case a => f(a) | |
} | |
inline def filter(f: A => Boolean): Option[A] = if (f(x)) x else null | |
inline def getOrElse(default: A): A = x match { | |
case null => default | |
case a => a | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment