[swift-evolution] ternary operator ?: suggestion

Charles Constant charles at charlesism.com
Thu Jan 7 01:15:55 CST 2016


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

You can't "if" anything other than a Bool.



On Wed, Jan 6, 2016 at 11:09 PM, John Randolph <jcr at icloud.com> wrote:

>
> > 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/265cabda/attachment.html>


More information about the swift-evolution mailing list