Skip to content

Instantly share code, notes, and snippets.

@bartosz-witkowski
Created December 29, 2014 14:22
Show Gist options
  • Save bartosz-witkowski/221244cce17ecf0604f1 to your computer and use it in GitHub Desktop.
Save bartosz-witkowski/221244cce17ecf0604f1 to your computer and use it in GitHub Desktop.
MaybeRef
object MaybeRef {
sealed abstract class Optional
implicit class MaybeRefFunctions[A](val dis: MaybeRef[A]) extends AnyVal {
def map[B](f: A => B): MaybeRef[B] = {
if (dis == null) {
null
} else {
Tag(f(Tag.unwrap(dis)))
}
}
def get(default: A): A = if (dis == null) default else Tag.unwrap(dis)
def notEmpty: Boolean = dis != null
def isEmpty: Boolean = dis == null
}
}
trait MaybeRefSyntax {
import MaybeRef._
def just[A](a: A): MaybeRef[A] = Tag(a)
def nothing[A]: MaybeRef[A] = null
}
@bartosz-witkowski
Copy link
Author

type MaybeRef[A] = A @@ MaybeRef.Optional

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