[swift-evolution] ternary operator ?: suggestion

Paul Ossenbruggen possen at gmail.com
Sun Dec 20 12:40:15 CST 2015

This is interesting, and thanks for the proposal, and I am not completely against something like this:

Your points on problems with ternary, I agree, except on point 2)  I would like to challenge a bit on the notion that ? is only for optionals, if it means that there is a question, instead of an optional. Then it fits pretty well. 

> On Dec 20, 2015, at 9:14 AM, Alexandre Lopoukhine <superlopuh at gmail.com> wrote:
> Hi all,
> I would like to suggest an alternative to this, and would like to hear what you all think. I think that the biggest problems with the current operators are:
> 1) They are not clear, by themselves or as a pair.
> 2) ? is associated with optionals
> 3) The type of the resulting expression is not immediately clear, as it begins as a boolean, and only after the ? are the actual expressions to be assigned
> So, using “booleanExpression”, “anExpression” and “alternativeExpression”:
> Current Swift:
> booleanExpression ? anExpression : alternativeExpression
> Paul’s suggested syntax:
> ?(booleanExpression: anExpression, alternativeExpression) 
> /* Shouldn’t this be ?(booleanExpression: true: anExpression, false: alternativeExpression), for it to be a true equivalent to switch, or should the boolean shorthand be hard-coded?

I would not want to have to spell out the true: false: With my proposal you can certainly do that and even reverse it if it fits your needs better:

?(cond : false: “A”, true: “B”)

Note: In my proposal you can put default: or else: if you prefer. 

?(cond : “A” else: “B”)

This might be a nice middle ground for terseness vs readability. I think flexibility is important so that developers have different ways of expressing it for what is best in the situation. 

> My suggestion:
> (anExpression if booleanExpression else alternativeExpression)
> The logic here is:
> 1) by adding the keywords, the expression becomes more verbose, but also clearer
> 2) the “?" is gone
> 3) it begins with an expression of the result type

We did discuss something like this earlier on. I think the required parenthesis is a good addition, this is part of my suggestion.  

> I think that requiring the parentheses might be a good idea, as it immediately flags the first expression as not being alone, especially if the expression is separated across multiple lines.
> Having “if” after the first expression also clearly distinguishes it as different from the “if” used for control flow.
> The obvious loss to Paul’s proposal is the loss of switch-level power, but I’m not convinced that this is too bad. Switch statements are great, but can grow quite long with various patterns, and I think should be kept (with current syntax required) as-is.

I think that would be a pretty big loss dropping switch support. Most of the examples in the Swift Language guide for the switch statement could more simply be written with a switch expression. Also, I think just replacing it with an if may not be considered a big enough benefit to make a change. I am pretty sure we could accommodate switch with something like your proposal too, if there is strong agreement that this is a better way to go.

> What do you think?

• Some downsides, we lose terseness which Chris and others brought up as important if trying to come up with something better. 
 • This is similar to python’s ternary while it reads pretty well as an English sentence, I am not sure it is better. Grouping the outcomes together I think is better. It feels odd and I remember when Python first introduced it, it was confusing. 

- Paul
> — Sasha

More information about the swift-evolution mailing list