[swift-evolution] ternary operator ?: suggestion

Lukas Stabe ahti333 at gmail.com
Sat Dec 5 18:32:51 CST 2015

> I don't think you can just get rid of the if statement in favor of an expression. You still want to be able to do this:
> if (condition) {
>     funcWithSideEffectsThatReturnsInt()
> } else {
>     funcWithSideEffectsThatReturnsString()
> }
> but that's not a valid expression (what is its type?).

An if statement with two different types could just have the closes common ancestor or Any as type.

> Yeah, returning an optional is a good idea.

This would, interestingly, make the else statement very similar to the optional-chaining operator ?? with the small difference that the second argument is a block (and does ?? autoclosure the second arg?).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/611fa694/attachment.html>

More information about the swift-evolution mailing list