# [swift-evolution] ternary operator ?: suggestion

Paul Ossenbruggen possen at gmail.com
Sat Dec 19 23:28:35 CST 2015

```Thank you for your feedback!

Just want to respond a little bit, I don’t believe it is any worse than the ternary in terms of it being non obvious as to what it does, and it adds a lot of power beyond the ternary, because now you can switch to more than two outcomes.

In terms of readability, it does improve on the ternary because I often find it hard to determine where a ternary begins, in unfamiliar code, looking at:

x = x == y ? 49 : 3

It is not until you get to the question mark that you realize it is a ternary and the x == y just is kind of floating there. The = and == just looks strange. Many do this to make it look better:

x = (x == y ? 49 : 3)

With the new form:

x = ?(x == y : 49, 3)

The x == y pops out more, similar to how adding the parenthesis helps, and you can immediately tell that this is part of a demux expression. This codifies the practice, of putting in parenthesis, in the same way that mandatory braces in control flow statements make the statements stand out. So I do believe it does address some of the problems with ternary.

If that is not enough of a difference from ternary to make this proposal fly, then maybe the hybrid approach outlined in Alternatives Considered section would be preferred? One example:

x = if(x == y : 49, 3)

This looks more function like and has many of the benefits of my original proposal and perhaps better addresses some of the downside of the ? being non obvious as to what it does at the expense of conciseness. Although, as Austin Zheng says, ? is for query, not necessarily just to indicate optional. If we don’t go with that interpretation of ?, this direction, may help to keep the concept of ? for optionals. Ternary kind of steps into that and makes it less separated so this alternative may be a better direction. I don’t object to this approach, if that is the consensus.

- Paul

> On Dec 19, 2015, at 7:55 PM, Jordan Rose <jordan_rose at apple.com> wrote:
>
> It's a nice, consistent proposal, but I don't feel like this solves any of the complaints about the existing ternary operator:
>
> - It's not obvious what it does when you first learn it.
> - The '?' doesn't have anything to do with Optionals.
>
> It is a way to put 'switch' into an expression. I'm not a fan of the two different colons, but that's "just" syntax.
>
> Jordan
>
>> On Dec 18, 2015, at 14:04 , Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> All,
>>
>> I think, I finally might have the answer to improving ternary, with such a bold statement come some pretty high expectations but I think, I might actually have done it this time :-)
>>
>> I am calling it the Demux Expression, it builds on the benefits of ternary and switch while improving on those.
>>
>> https://github.com/possen/swift-evolution/blob/master/proposals/0024.md
>>
>> This is a first draft, thanks in advance for feedback!
>>
>> - Paul
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>

```