[swift-evolution] ternary operator ?: suggestion

Jakob Egger jakob at eggerapps.at
Sun Dec 13 10:11:12 CST 2015

I really don't like the "if cond then expr1 else expr2".

1) First of all, it's extremely verbose. It's almost as bad as the SQL construct "case when cond then expr1 else expr2 end".

2) It makes Swift harder to learn. Newcomers will be confused why you sometimes need to use curly braces and why at other times you need to use the "then" keyword.

3) It doesn't help when you want to do multi-line calculations

But there is one good side to this proposal: it nests more naturally than the ternary operator:
"if cond then expr1 else if cond2 then expr2 else expr3"
"cond ? expr1 : (cond2 ? expr2 : expr3)

The proposed syntax for the switch statement is so confusing that I really don't think it is a good idea.

Why not just allow normal if statements to return values?
I don't see what's so bad about the following:
"let value = if cond {expr1} else {expr2}"

This would also extend to multiple lines:
let value = if cond {
} else {
  let something = Object()


> On 13 Dec 2015, at 06:54, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> wrote:
> Hello All, 
> Been sick in bed all day, but decided to try to be productive…
> I did a rough draft of a proposal for implementing if expressions and switch expressions based upon the discussions we had here. I have tried to keep the scope of the changes as small as possible,  only added one keyword and kept things as similar to the existing language constructs as possible. If anyone wants to help me with this, or has feedback, please let me know,
> https://github.com/possen/swift-evolution/blob/master/0020.md <https://github.com/possen/swift-evolution/blob/master/0020.md>
> Thanks,
> - Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151213/6c1dcf92/attachment.html>

More information about the swift-evolution mailing list