[swift-evolution] ternary operator ?: suggestion
Paul Ossenbruggen
possen at gmail.com
Thu Jan 7 00:05:05 CST 2016
I suppose that you could interpret : as a separator, or you could interpret it as else or default. I have chosen the latter. It is up to us to decide how to interpret it because the ternary only has true and false.
let fe = truthy == truth ? “unlikely” : “likely”
Some other nice things about using it as the default or else is that you can support syntaxes like the zero based index based one.
let fb = pickOne ? “A", "B", "C", "D", "E", "F", "G” : "Z"
this may be a non goal for some but I really think it could come in handy. Where the “Z” is selected if not 0-6. If we use the colon as the default or else case then this is possible. This does not break with the ternary tradition if you interpret the colon as default or else.
Also, nicely, the switch form does not conflict with the last colon if we use the underscore to exhaustively list the cases because it technically does not need the : to be the default. So in that form, you would not have the default.
Also, I agree with the sentiment that i would rather get something than nothing, even if that means having to have the redundancy of cases and default: but I really don’t like extra boilerplate and there is a risk that it won’t seem as big a win if you have to list all the cases out.
> On Jan 6, 2016, at 9:07 PM, Charles Constant <charles at charlesism.com> wrote:
>
> 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 <mailto: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 <mailto: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/42840e1e/attachment.html>
More information about the swift-evolution
mailing list