[swift-evolution] ternary operator ?: suggestion

Thorsten Seitz tseitz42 at icloud.com
Sat Dec 12 06:28:40 CST 2015


I like these expressions with "then" and without braces.

What about using "then" for statements as well?

If condition then {
        ...
} else {
        ...
}

> Am 12.12.2015 um 08:15 schrieb Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org>:
> 
> 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> wrote:
>>> 
>>>> On Dec 11, 2015, at 5:36 PM, Andrey Tarantsov via swift-evolution <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
>> 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/193181d3/attachment.html>


More information about the swift-evolution mailing list