[swift-evolution] ternary operator ?: suggestion

Taras Zakharko taras.zakharko at uzh.ch
Sun Dec 13 10:20:05 CST 2015

I must say I agree with Jakob. Adding a ternary expression if variant only makes the language more complex (in every sense of this word) and does not really solve anything.  A general ‘if-as-expression’ is a more ‘sustainable’ solution and more inline with Swift3 spirit of simplification. 



> On 13 Dec 2015, at 17:11, Jakob Egger via swift-evolution <swift-evolution at swift.org> wrote:
> 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"
> vs.
> "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 {
>   expr1
> } else {
>   let something = Object()
>   something.doStuff()
>   something
> }
> Jakob
>> On 13 Dec 2015, at 06:54, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto: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
>  _______________________________________________
> 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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151213/9b51befb/attachment.html>

More information about the swift-evolution mailing list