[swift-evolution] ternary operator ?: suggestion

Al Skipp al_skipp at fastmail.fm
Sat Dec 12 06:03:54 CST 2015


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> 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
> 
> 
> _______________________________________________
> swift-evolution mailing list
> 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/b8d068f0/attachment.html>


More information about the swift-evolution mailing list