[swift-evolution] ternary operator ?: suggestion
Paul Ossenbruggen
possen at gmail.com
Tue Dec 22 11:54:33 CST 2015
This is interesting, the “which" out front helps to keep conditional from just “floating" as it does with the ternary and using the square brackets and a dictionary like sequence is pretty cool. I did think a bit along these lines too. You could have a Array like and Dictionary like form. The melding of the nil-coalescing operator is also a nice way to deal with the else part. Not sure “which” works with the array form but perhaps.
i really would like to support pattern matching. I also have concerns about the different but not better comments I got.
- Paul
> On Dec 22, 2015, at 7:04 AM, Félix Cloutier via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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.
>
> Félix
>
>> 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
>> }
>>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list