<!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> </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> </div>
<div>if (condition) {<br></div>
<div> funcWithSideEffectsThatReturnsInt()<br></div>
<div>} else {<br></div>
<div> funcWithSideEffectsThatReturnsString()<br></div>
<div>}<br></div>
<div> </div>
<div>but that's not a valid expression (what is its type?).<br></div>
</div>
</div>
</blockquote><div> </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> </div>
<div>That's a great way to cause confusion.<br></div>
<div> </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> </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> </div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">if condition {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> funcWithSideEffectsThatReturnsInt();<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}<br></span></div>
<div> </div>
<div>but it would be confusing because semicolons are almost never used in Swift.<br></div>
<div> </div>
<div>An alternative that would work today is just relying on assignment returning Void, so you can write<br></div>
<div> </div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">if condition {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> _ = funcWithSideEffectsThatReturnsInt()<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}<br></span></div>
<div> </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> </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> </div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">if condition {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> funcWithSideEffectsThatReturnsInt()<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">} else {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> funcWithSideEffectsThatReturnsString()<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}</span></div>
<div> </div>
<div>and it would be fine but writing<br></div>
<div> </div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">let foo = if condition {</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> funcWithSideEffectsThatReturnsInt()</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">} else {</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> funcWithSideEffectsThatReturnsString()</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}</span><br></div>
<div> </div>
<div>would fail with a type error.<br></div>
<div> </div>
<div>I suspect that this is the right approach, but it does involve a bit of magic.<br></div>
<div> </div>
<div>-Kevin</div>
</body>
</html>