Created
October 22, 2012 00:05
-
-
Save mdread/3929025 to your computer and use it in GitHub Desktop.
An Elvis operator aware of empty strings
This file contains hidden or 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 ElvisOP { | |
implicit def any2elvis(x: Any) = new ElvisOP(Option(x)) | |
} | |
class ElvisOP(me: Option[Any]) { | |
def ?> (that: Any) = me match{ | |
case Some(s: String) => s match { | |
case _ if s.isEmpty() => that | |
case _ => s | |
} | |
case Some(b: Boolean) => if(!b) that else b | |
case Some(s) => s | |
case None => that | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment