[swift-evolution] ternary operator ?: suggestion

Charles Constant charles at charlesism.com
Wed Jan 6 23:07:22 CST 2016


Hi Paul,

My opinion, atm, is that ":" as a separator is the best solution. True,
it's not *Swift-like*, but neither is the existing ternary, which Chris
wants to keep. On then other hand, "colon as separator" is extremely
*ternary-like*.

So benefits of "colon as separator" as I see them:

- Syntax is *easy to remember* (as long as you're used to writing ternary
expressions)
- Extremely *compact* syntax
- Each component is demarked by a special character, which I'm pretty sure
makes any *chaining* possible
- *Easy to "market"* as a "new modification of ternary expressions in
Swift" rather than a brand new construct
- *Somewhat intuitive*. The compact form and cryptic symbols mean a ternary
isn't going to be self explanatory. But at least there's a call and
response sort of logic to "condition *?*" followed by "case *!*"

That said, I don't want to dig my heels in about "colon as separator"
version. If others don't like it, I support *any* version we've discussed
in this thread, switch-like or ternary-like. As long as it means we do away
with a preceding variable declaration and some of the repeated
boilerplate... that's the important thing for me.


















On Wed, Jan 6, 2016 at 8:35 PM, Paul Ossenbruggen <possen at gmail.com> wrote:

> Hi Charles,
>
> Chris, already said he did not like ? being used for ternary because it
> was already used for optionals. I think because of historical precedent it
> may be acceptable here.  I have tried combos earlier with ! and got no
> support, admittedly not with the same usage as yours.
>
> Do you really find it confusing not having a separator there? It is
> essentially a switch case without the word “case”. In my model the colon
> only indicates the “else” or “default” case, the other cases are separated
> by commas or whitespace. With my email taking the Swift Book examples and
> converting it from statement to expression form, I did not find any of them
> confusing, well maybe the first one, which found vowels and consonants, but
> that did not look that great in statement form either. .
>
> if so maybe the | would be better as the separator because it would not
> change the switch like syntax:
>
> let numberSymbol: Character = "三"  // Simplified Chinese for the number 3
> let possibleIntegerValue:Int? = numberSymbol ?
>    | "1", "١", "一", "๑": 1
>    | "2", "٢", "二", "๒": 2
>    | "3", "٣", "三", "๓": 3
>    | "4", "٤", "四", "๔": 4
>    |  _ : nil
>
> if let integerValue = possibleIntegerValue {
>     print("The integer value of \(numberSymbol) is \(integerValue).")
> } else {
>     print("An integer value could not be found for \(numberSymbol).")
> }
> // prints "The integer value of 三 is 3.”
>
> But I still don’t see it a necessary.  perhaps that would allow removal of
> the parenthesis and I think this would get rejected as not looking very
> Swift like. Also single line form which isn’t bad:
>
> * let val = color ? **.Red:** 0xFF0000 | **.Green:** 0x00FF00 | * *.Blue:*
> * 0x0000FF** | **_:** 0xFFFFFF*
>
> But still not sure the | adds that much.
>
> * let val = color ? **.Red:** 0xFF0000, **.Green:** 0x00FF00, * *.Blue:*
> * 0x0000FF, * *_:** 0xFFFFFF*
>
> Or this is without the commas is still readable but maybe a little harder:
>
> * let val = color ? **.Red:** 0xFF0000 **.Green:** 0x00FF00 * *.Blue:*
> * 0x0000FF * *_:** 0xFFFFFF*
>
> One other thing, I would still prefer the control value inside the brace
> rather than out front, but I see that most people still want it out front
> and since it would be a breaking change for ternary, I have kind of backed
> off it but I think this is still clearer because the control value is not
> floating out in front.
>
> * let val =  ?(color, **.Red:** 0xFF0000, **.Green:** 0x00FF00, * *.Blue:*
> * 0x0000FF, * *_:** 0xFFFFFF)*
>
> On Jan 6, 2016, at 7:06 PM, Charles Constant <charles at charlesism.com>
> wrote:
>
> > I see what you are trying to do, because of the colon being both used
> for switch cases and
> > separators for the ternary and so there needs to be a new character for
> each case.
> > I am not sure that putting colons between each case is really necessary
> though.
>
> Most of us (including you and I) like a form that starts with " let val =
> condition ? " like the existing ternary. Let's say a proposal like that
> gets accepted... I really believe "colons as separators" is the best idea
> in the case. Otherwise, it gets pretty confusing.. we'll have the existing
> ternary where a colon does one thing, and our new "extra ternary" where it
> does something else.
>
> This is why I like colons (this won't make sense unless your email has
> rich text to show the colors):
>
> * let val = color ? *
> *.Red !**  0xFF0000 : *
> *.Green !**  0x00FF00 : *
>
> *_ !**  0xFFFFFF*
>
> ... no syntax here different from the existing except the addition "
> *.Red !* ". As for the exclamation... Swift already uses an exclamation
> for a billion other things, which is unfortunate. But the same can be said
> of "?" and that's already used in a "switch" without causing confusion.
>
> > To point 1: I agree it needs a new name, I came up with the “demux
> expression”
> > but maybe there is a better name.
>
> Has anyone suggested "multiary expression" yet? Seems in keeping with
> "ternary"
>
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/bd6291d5/attachment.html>


More information about the swift-evolution mailing list