Created
June 12, 2021 13:14
-
-
Save markehammons/9c964a5ecefc84913f40e01a91b9ffee to your computer and use it in GitHub Desktop.
Opaque Option type
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
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