[swift-evolution] ternary operator ?: suggestion

Thorsten Seitz thorsten.seitz at web.de
Sun Dec 6 08:22:06 CST 2015

Wouldn’t it be possible to still have if-statements?

When used as expression an if-expression would of course require an else-part, but when used as statement we would still have an if-statement (not an if-expression) and the else-part would be optional just like now.


> Am 06.12.2015 um 00:12 schrieb Ole Begemann via swift-evolution <swift-evolution at swift.org>:
>>> This is subtle and requires thought
>> What do you envision as the pitfalls of the design thinking behind if/switch expressions? I’m not a compiler programmer, but a prog-lang enthusiast. I wouldn’t mind a little nudge in the right direction.
> One thing that comes to mind is that if "if" is an expression, every if needs an else branch. This makes it harder to use if to conditionally perform side effects. For example, this is currently valid:
> func doSomething() { ... }
> if condition {
>    doSomething()
> }
> Should this be allowed? You could argue that this should work because (a) the expression result is unused and (b) the return type of doSomething is Void, so the compiler could conceivably implicitly construct an else branch that returns (). But it would be inconsistent with other expressions. Disallowing this style could make writing typical "imperative" code harder.
> – Ole
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

More information about the swift-evolution mailing list