[swift-evolution] ternary operator ?: suggestion

Félix Cloutier felixcca at yahoo.ca
Tue Dec 22 09:04:07 CST 2015

I like the gist of it too, though you seem to introduce both a new keyword and a new syntax. (To be clear, I like the syntax but I'm ambivalent towards reusing switch instead of which.)

My minor suggestions would to avoid braces for things that aren't scopes; that either the comma or the the question mark is redundant in their current position (you need a start delimiter or an end delimiter but you don't need both); and that it needs a way to handle a default case if enumeration isn't exhaustive (I'd do that by returning an optional).

> let i = which color (.Red: 0xff0000, .Green: 0x00ff00, .Blue: 0x0000ff) ?? 0x000000

Thinking out loud, once you remove the question marks it really looks like a dictionary literal, so maybe it could even use square brackets to close the gap.

> let i = which color [.Red: 0xff0000, .Green: 0x00ff00, .Blue: 0x0000ff] ?? 0x000000

I thought about subscripting a dictionary literal in place:

> [Color.Red: 0xff0000, ...][color] ?? 0x000000

but that won't support elaborate pattern matching, and I think that this is a deal breaker for the functional folks.


> Le 22 déc. 2015 à 09:31:32, Charles Constant <charles at charlesism.com> a écrit :
> Just goofing on this a little. What if we called it a "which" statement, instead of a "switch" statement? It's a bit cutesy, but not too verbose, and it makes sense if you read it aloud.
> let i = which color {
> 	? .Red: 0xFF0000, 
> 	? .Green: 0x00FF00, 
> 	? .Blue: 0x00000FF
> }
> let i = which boo {
> 	? true: 1, 
> 	? false: 0, 
> 	? nil: -1
> }

More information about the swift-evolution mailing list