I’m writing a code generator which produces Scala output.
I need to emulate a ternary operator in such a way that the tokens leading up to ‘?’ remain intact.
e.g. convert the expression c ? p : q to c something. The simple if(c) p else q fails my criteria, as it requires putting if( before c.
My first attempt (still using c/p/q as above) is
c match { case(true) => p; case _ => q }
another option I found was:
class ternary(val g: Boolean => Any) { def |: (b:Boolean) = g(b) }
implicit def autoTernary (g: Boolean => Any): ternary = new ternary(g)
which allows me to write:
c |: { b: Boolean => if(b) p else q }
I like the overall look of the second option, but is there a way to make it less verbose?
Thanks
Even though the syntax doesn’t evaluate in the expected order–it binds the conditional to the first option!–you can make your own ternary operator like this:
The trick is to interpret
?as a method on aMakeIfTrueobject that binds the condition to the object to return in the “true” case. The resultingIfTrueobject now uses the|method as a request to evaluate the condition, returning the stored true option if the condition is true, or the just-passed-in one if it’s false.Note that I’ve used stuff like
=> Ainstead of justA–by-name parameters–in order to not evaluate the expression unless it’s actually used. Thus, you’ll only evaluate the side that you actually need (just like an if statement).Let’s see it in action:
(P.S. To avoid confusion with the Actor library
?, you probably ought to call it something else like|?.)