[swift-evolution] ternary operator ?: suggestion
Al Skipp
al_skipp at fastmail.fm
Sun Dec 13 10:58:10 CST 2015
> On 13 Dec 2015, at 16:11, Jakob Egger via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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}"
I’d be happy if the ‘if’ and ‘switch’ statements could somehow also behave as expressions. One problem is that an ‘if’ statement doesn’t require an ‘else’ clause, whereas an expression does.
Taras, earlier mentioned a possible solution, which would return ‘nil’ if the else clause was absent. I think any suggestion must have compile time checking to prevent runtime crashes occurring.
> This would also extend to multiple lines:
> let value = if cond {
> expr1
> } else {
> let something = Object()
> something.doStuff()
> something
> }
My personal preference here is that ‘if’ expressions should aim to fit onto one line and avoid invoking side-effecting functions. However, Swift isn’t a language which controls side-effects within the type system, so maybe my point is moot?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151213/71c1a303/attachment-0001.html>
More information about the swift-evolution
mailing list