Skip to content

Instantly share code, notes, and snippets.

@markehammons
Created June 12, 2021 13:14
Show Gist options
  • Save markehammons/9c964a5ecefc84913f40e01a91b9ffee to your computer and use it in GitHub Desktop.
Save markehammons/9c964a5ecefc84913f40e01a91b9ffee to your computer and use it in GitHub Desktop.
Opaque Option type
opaque type Option[T] = T | Null
object Option:
def apply[T](t: T): Option[T] = t
def None: Option[Nothing] = null
extension [T](t: Option[T])
def map[U](fn: T => U): Option[U] =
if t == null then None else fn(t.asInstanceOf[T])
def flatMap[U](fn: T => Option[U]): Option[U] = if t == null then None else fn(t.asInstanceOf[T])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment