Created
December 29, 2014 14:22
-
-
Save bartosz-witkowski/221244cce17ecf0604f1 to your computer and use it in GitHub Desktop.
MaybeRef
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
type MaybeRef[A] = A @@ MaybeRef.Optional