Skip to content

Instantly share code, notes, and snippets.

@nicktelford
Created June 26, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicktelford/876bd6cb94733f795f8167a74bebb7ce to your computer and use it in GitHub Desktop.
Save nicktelford/876bd6cb94733f795f8167a74bebb7ce to your computer and use it in GitHub Desktop.
Cheap Options in dotty?
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