[swift-evolution] ternary operator ?: suggestion

Dennis Lysenko dennis.s.lysenko at gmail.com
Mon Dec 14 18:10:16 CST 2015


Good point Jordan. I think what Jakob was trying to say is that 'else if'
is already a language construct, and thus it might be easier on the eye to
see 'if cond then expr1 else if cond2 then expr2 ...', than to see 'cond1 ?
expr1 : cond2 ? expr2 : ...'

On Mon, Dec 14, 2015 at 6:38 PM Jordan Rose via swift-evolution <
swift-evolution at swift.org> wrote:

>
> On Dec 13, 2015, at 8:11, Jakob Egger via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> But there is one good side to this proposal: it nests more naturally than
> the ternary operator:
> "if cond then expr1 else if cond2 then expr2 else expr3"
> vs.
> "cond ? expr1 : (cond2 ? expr2 : expr3)
>
>
> Without commenting on anything else in this thread, ?: nests properly
> (right-associative-ly) in every language other than PHP
> <http://www.phpsadness.com/sad/30>. Dropping the parens there is
> equivalent.
>
> (That said, if I hadn't known that already it wouldn't have been something
> I could infer from the syntax. I'd have to assume the author knew what they
> were doing. And I'm the sort who will parenthesize && expressions within ||
> expressions, even though it's not strictly necessary.)
>
> Jordan
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/99df6d6b/attachment.html>


More information about the swift-evolution mailing list