[swift-evolution] ternary operator ?: suggestion

Paul Ossenbruggen possen at gmail.com
Sat Dec 12 08:48:51 CST 2015


Dropping “case” might be interesting, since it becomes a little redundant in the “switch” situation, this is one advantage of having a new keyword, but not sure this reads as well:

let v = switch val then .Red: 1, .Green: 2, .Blue: 3

It is definitely nice in it’s compactness which is a big plus. 

Another possibility, because “switch" does not need to resolve the syntactic ambiguity, but then we lose the “then” always meaning an expression consistency. 

let v = switch val case .Red: 1, case .Green: 2, case .Blue: 3

this might be better for switch because we don’t need to mix “then” with “switch” which historically has not been done. Question is, is it better to go with “then” as expression consistency or with slightly more compact and following the conventions out there. Personally, I am not bothered by using “then” with “switch” 


> On Dec 12, 2015, at 4:03 AM, Al Skipp <al_skipp at fastmail.fm> wrote:
> 
> If at all possible, it’d be good to avoid adding new keywords, however if the keyword ‘then’ could enable 'if expressions’ and ‘switch expressions’, I’d be fully in favour. 
> 
> Binding to a variable using a switch statement isn’t as elegant as it could be. The ability to define a ‘let’ variable and assigning a value later in a switch statement does help somewhat, but the examples below are very clear and elegant in my opinion. (I’d even question the necessity of the repetitive keyword ‘case’, but maybe that’s a step too far?)
> 
> Al
> 
>> On 12 Dec 2015, at 07:15, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> So adding “then”: 
>> 
>> let v = if condition then “A” else “B” 
>> 
>> fixes that ambiguity and “then” would help differentiate an expression from a statement. I think there is value to having them separate constructs (for details see earlier comments in this thread). Also it is nice not having the braces in the expression. 
>> 
>> Perhaps, similarly, “then" indicates an expression as opposed to a statement:
>> 
>> let v = switch condition then case Red: 1, case Blue: 2, case Green: 3
>> 
>> One downside is it, It may add confusion as to when to add the “then” clause as people will have to know what an expression vs statement is, but I suppose that is true with a ternary operator as well, and this reads better than a ternary operator. This also provides a good way to do single line switch expressions. 
>> 
>> for multiline:
>> 
>> let v = if condition 
>> 	then “A” 
>> 	else “B"
>> 
>> let v = switch condition then 
>> 	case .Red: 1,
>> 	case .Blue: 2,
>> 	case .Green: 3
>> 
>> or with multiple expressions:
>> 
>> let v = switch condition then 
>> 	case .Red: 1,
>> 	case .Blue: (if shade == .Dark then 4 else 2),
>> 	case .Green: 3
>> 
>> 
>>> 
>>> On Dec 11, 2015, at 8:57 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>>> On Dec 11, 2015, at 5:36 PM, Andrey Tarantsov via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>>> How about:
>>>>> 
>>>>> let v = if condition then “A" else “B"
>>>> 
>>>> I don't think introducing a separate "then" keyword is a good idea, two subtly different kinds of IFs would be confusing.
>>>> 
>>>> I guess you meant:
>>>> 
>>>> let v = if condition { "A" } else { "B” }
>>> 
>>> I don’t really want to wade into this discussion, but if A and B are intended to be *expressions* instead of an arbitrary sequence of statements|decls|exprs, then a more consistent syntax would be:
>>> 
>>> 	let v = if condition (A) else (b) 
>>> 
>>> The immediate problem with that is that juxtaposition of two expressions (condition, and A [with or without parens]) will lead to immediate syntactic ambiguity.
>>> 
>>> -Chris
>>> 
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151212/3d8e9266/attachment.html>


More information about the swift-evolution mailing list