[swift-evolution] ternary operator ?: suggestion

John Randolph jcr at icloud.com
Thu Jan 7 01:09:58 CST 2016


> On Jan 6, 2016, at 10:18 PM, Charles Constant via swift-evolution <swift-evolution at swift.org> wrote:
> 
> let fb = pickOne ? “A" : "B" : "C" : "D" : "E" : "F" : "G” : _ ! "Z"
> 

I think it’s worth mentioning that in C today (both in GCC and Clang), we can use the ternary operator in a shortened fashion like so:

	a = x ?: y;

which is equivalent to: 

	a = x ? x : y;


Meaning, assign x to a if x is non-zero, otherwise make it equal to y.

This construct occurs all over the place in my Obj-C code, and porting that to Swift today is perfectly straightforward.  If the ?: operator becomes the ?:::::: operator, then does this very common shorthand use of the existing ternary operator become impossible?

I’m for keeping the ternary operator as it is.  Expanding it into a poor man’s switch() is unnecessary and has definite drawbacks.

-jcr


More information about the swift-evolution mailing list