[swift-evolution] ternary operator ?: suggestion

Alex Popov hello at alexpopov.ca
Wed Jan 6 23:10:01 CST 2016


I like the bar `|` variation, but I do think the control value should be out front, otherwise the ? reads as an optional to my mind, whereas the former comes across as the beginning of a ternary (which this is effectively an extension of). The bar variation feels Haskell-y in a satisfying way, and also has the added benefit of clearly delimiting the effective "range" of the multiary. 

Alex Popov Jr.
Principal iOS developer | Shelfie
www.shelfie.com | @getshelfie




On Wed, Jan 6, 2016 at 8:35 PM -0800, "Paul Ossenbruggen via swift-evolution" <swift-evolution at swift.org> 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 3let 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/20160107/fb18cb3f/attachment.html>


More information about the swift-evolution mailing list