Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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"
}

Wicked.

Very wicked indeed.

gregghz commented May 21, 2017 edited

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