<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Sat, Dec 5, 2015, at 04:32 PM, Lukas Stabe via swift-evolution wrote:<br></div>
<blockquote type="cite"><div>&nbsp;</div>
<div><blockquote type="cite"><div>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:<br></div>
<div><div dir="ltr"><div>&nbsp;</div>
<div>if (condition) {<br></div>
<div>&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt()<br></div>
<div>} else {<br></div>
<div>&nbsp; &nbsp; funcWithSideEffectsThatReturnsString()<br></div>
<div>}<br></div>
<div>&nbsp;</div>
<div>but that's not a valid expression (what is its type?).<br></div>
</div>
</div>
</blockquote><div>&nbsp;</div>
<div>An if statement with two different types could just have the closes common ancestor or Any as type.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>That's a great way to cause confusion.<br></div>
<div>&nbsp;</div>
<div>Rust has this feature (all statements are expressions), and it requires if statements to have an else branch with the same type unless the type is `()`. It's solution to the issue of the branches returning unwanted values is that Rust uses semicolons, and the semicolon acts sort of like an operator that consumes any value and returns `()`, so if you terminate the last statement of the branch with a semicolon, the whole branch returns `()`, and if you leave it off, the branch returns a value. It's actually very elegant and straightforward.<br></div>
<div>&nbsp;</div>
<div>That said, proposing that Swift introduce this same rule for semicolons is probably not a good idea. We certainly could declare that an explicit semicolon has this behavior, so you'd see people writing code like<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">if condition {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt();<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}<br></span></div>
<div>&nbsp;</div>
<div>but it would be confusing because semicolons are almost never used in Swift.<br></div>
<div>&nbsp;</div>
<div>An alternative that would work today is just relying on assignment returning Void, so you can write<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">if condition {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; _ = funcWithSideEffectsThatReturnsInt()<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}<br></span></div>
<div>&nbsp;</div>
<div>but that looks kind of weird and would probably also be confusing. Better than the semicolon rule I think, but still not great.<br></div>
<div>&nbsp;</div>
<div>Another option is to check if the return value is actually used anywhere, and if it's not, then silently coerce it to Void. This way you can write<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">if condition {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt()<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">} else {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; funcWithSideEffectsThatReturnsString()<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span></div>
<div>&nbsp;</div>
<div>and it would be fine but writing<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">let foo = if condition {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt()</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">} else {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp;&nbsp;funcWithSideEffectsThatReturnsString()</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><br></div>
<div>&nbsp;</div>
<div>would fail with a type error.<br></div>
<div>&nbsp;</div>
<div>I suspect that this is the right approach, but it does involve a bit of magic.<br></div>
<div>&nbsp;</div>
<div>-Kevin</div>
</body>
</html>