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
// --- Shorter with Option[T] --- // | |
implicit class WhenUnless[T](x: => T) { | |
lazy val xx = x | |
def when(cond: => Boolean):Option[T] = if(cond) Some(xx) else None | |
def unless(cond: => Boolean):Option[T] = if(cond) None else Some(xx) | |
} | |
implicit class Otherwise[+T](val x: Option[T]) extends AnyVal { | |
def otherwise[S >: T](z: => S):S = if(x.isDefined) x.get else z //could not use getOrElse |