[swift-evolution] ternary operator ?: suggestion

possen p possen at gmail.com
Sun Dec 6 04:14:10 CST 2015


(I originally posted as possen p and since hopefully fixed that) 

So, As Kevin Ballard and Ole Bergman pointed out, making a regular “if” an expression has some major complications. Having the potential to return different object types in a strictly typed language will not be easy. Also what to do if the if does not have an else. The ternary operator does not allow the if/else results of different types. In Python doing an expression with if/else is not a problem because it will let you return different types, this is legal, (which makes sense for Python):

val = 10 if p == 5 else “abc"

it will not however let you omit the else part:

val = 10 if False  // produces an error. 

so it appears if you assign to an expression in Python it will require the else part. So it is different than a regular if. 

So I ask, is making the regular “if" an expression truly desired for Swift? I do see other languages doing it, but it does seem a bit odd to assign from any “if”. In my original proposal I was suggesting that assignment is done like this:

let val = 10 else 40 if p == 5 

In this it is very similar to the ternary expression in that else part is required and both types must match, and has improved readability. This is overloading the if/else keywords to provide essentially the same thing as ternary operators.. 

The following from Kevin Ballard's email:

> let foo = if condition {
>     funcWithSideEffectsThatReturnsInt()
> } else {
>     funcWithSideEffectsThatReturnsString() 
> }


I find it is hard to tell that the two results are returning a value. To me it looks like it is either ignoring the return value or is returning void. 

Maybe emphasizing the assignment would help as below does not have return values in the method names: 

let foo if condition {
    = dosomething()
} else {
    = dosomethingelse() 
}

putting the = after the else shows that it is returning a value for the let clause. It also helps emphasize that the return types must match and I think looks better. 

Going back to my proposal with the = suggestion: 

let foo = dosomething() else = dosomethingelse() if condition

this really shows that the let variable is being assigned. 

Note that these are still chainable expressions: 

var foo = ((10 else = 40 if p == 5) else = (15 else = 14 if p == 10) if p == 4)

It may be good to make the space optional after the else:

var foo = ((10 else= 40 if p == 5) else= (15 else= 14 if p == 10) if p == 4)

- Paul 
















-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/7a405aed/attachment.html>


More information about the swift-evolution mailing list