Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby-style trailing conditionals in Scala
class Conditional[T](left: => T) {
def iff(right: => Boolean): Option[T] = if (right) Some(left) else None
def unless(right: => Boolean): Option[T] = if (!right) Some(left) else None
}
implicit def any2Unless[T](left: => T) = new Conditional(left)
scala> val k = 1001
k: Int = 1001
scala> k iff k % 2 == 0
res9: Option[Int] = None
scala> k unless k % 2 == 0
res10: Option[Int] = Some(1001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.