Created
July 16, 2010 17:54
-
-
Save machisuji/478677 to your computer and use it in GitHub Desktop.
trailing conditionals
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
implicit def trailingConditionals[T](any: => T) = new { | |
def provided(expr: Boolean) = if (expr) Some(any) else None | |
def unless(expr: Boolean) = if (!expr) Some(any) else None | |
} | |
implicit def verbosePlus(i: Int) = new { | |
def plus(j: Int): Int = { | |
println("Adding " + i + " and " + j); | |
i + j | |
} | |
} |
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
scala> :load implicits.scala | |
Loading implicits.scala... | |
trailingConditionals: [T](any: => T)java.lang.Object{def provided(expr: Boolean): Option[T]; def unless(expr: Boolean): Option[T]} | |
verbosePlus: (i: Int)java.lang.Object{def plus(j: Int): Int} | |
scala> println("3 is larger than 1") provided 3 > 1 | |
3 is larger than 1 | |
res0: Option[Unit] = Some(()) | |
scala> println("1 is larger than 3") provided 1 > 3 | |
res1: Option[Unit] = None | |
scala> 1 plus 3 unless 1 > 3 | |
Adding 1 and 3 | |
res2: Option[Int] = Some(4) | |
scala> 3 plus 1 unless 3 > 1 | |
res3: Option[Int] = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment