Skip to content

Instantly share code, notes, and snippets.

@noelwelsh
Created April 17, 2015 10:38
Show Gist options
  • Save noelwelsh/02b9ecf6d21c7b8934e8 to your computer and use it in GitHub Desktop.
Save noelwelsh/02b9ecf6d21c7b8934e8 to your computer and use it in GitHub Desktop.
Nullable types in Scala
object Nullable {
sealed trait NullableTag
type Nullable[A] = A with NullableTag
def nullAs[B]: Nullable[B] =
null.asInstanceOf[Nullable[B]]
implicit class ToNullable[A](val a: A) extends AnyVal {
def `?`: Nullable[A] = a.asInstanceOf[Nullable[A]]
}
implicit class NullableOps[A](val a: Nullable[A]) extends AnyVal {
def map[B](f: A => B): Nullable[B] =
if(a == null) {
nullAs[B]
} else
f(a).?
def flatMap[B](f: A => Nullable[B]): Nullable[B] =
if(a == null)
nullAs[B]
else
f(a).?
def fold[B](empty: B)(full: A => B): B =
if(a == null)
empty
else
full(a)
def getOrElse(empty: A): A =
if(a == null)
empty
else
a
}
}
object Examples {
import Nullable._
def notNull =
("hi").? map (_ ++ " there") flatMap (a => if(a.length < 2) null else "dawg".?) getOrElse "dang"
def isNull =
("").? map (_ ++ "") flatMap (a => if(a.length < 2) null else "yeah".?) getOrElse "dang"
}
@FranklinChen
Copy link

Wicked.

@djspiewak
Copy link

Very wicked indeed.

@gregghz
Copy link

gregghz commented May 21, 2017

getOrElse should probably be: def getOrElse(empty: => A): A = and fold: def fold[B](empty: => B)(full: A => B): B =

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment