DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Trivial Ternary Operator In Scala
//Just a quick test/exploration of Pimp My Library to reproduce the syntax of the C++ et al ternary operator
object TernaryOp {
implicit def fakeTernary[A](a:(A,A)) = new{
def ?:(p:Boolean)=if(p) a._1 else a._2
}
//Before adding the Tuplicator class the compiler got very confused
// between anonymous classes
class Tuplicator[A](a:A){
def ~:(b:A) = (b,a)
}
implicit def tuplicate[A](a:A) = new Tuplicator(a)
}
object TernaryTest {
import TernaryOp._
def main(args: Array[String]) = {
println( (1>3) ?: 1 ~: 3 )
}
}





