<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I agree.<div class=""><br class=""></div><div class="">I’m sure there are use-cases where the different branches return different types, both conforming to some protocol. Since we’d need to support these cases, this approach is also pretty clean, because Any is just another protocol.</div><div class=""><br class=""></div><div class="">The warning emitted should be silenceable by explicitly declaring the variable being assigned to as Any.</div><div class=""><br class=""></div><div class="">Lukas</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 06 Dec 2015, at 05:46, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">That approach seems fine to me; I don't think it seems like magic.<div class=""><br class=""></div><div class="">"<font face="monospace, monospace" class="">if x { returnsAnInt() } else { returnsAString() }</font>" &nbsp;would have type Any, but would only emit a warning if you actually tried to <b class="">use</b> the value. Much like the current warning, "<i class="">X inferred to have type Any, which may be unexpected</i>".</div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class=""><div dir="ltr" class=""><div class="">Jacob Bandes-Storch<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote">On Sat, Dec 5, 2015 at 7:53 PM, Kevin Ballard via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u class=""></u>




<div class=""><span class=""><div class="">On Sat, Dec 5, 2015, at 04:32 PM, Lukas Stabe via swift-evolution wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">&nbsp;</div>
<div class=""><blockquote type="cite" class=""><div class="">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 class=""></div>
<div class=""><div dir="ltr" class=""><div class="">&nbsp;</div>
<div class="">if (condition) {<br class=""></div>
<div class="">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt()<br class=""></div>
<div class="">} else {<br class=""></div>
<div class="">&nbsp; &nbsp; funcWithSideEffectsThatReturnsString()<br class=""></div>
<div class="">}<br class=""></div>
<div class="">&nbsp;</div>
<div class="">but that's not a valid expression (what is its type?).<br class=""></div>
</div>
</div>
</blockquote><div class="">&nbsp;</div>
<div class="">An if statement with two different types could just have the closes common ancestor or Any as type.<br class=""></div>
</div>
</blockquote><div class="">&nbsp;</div>
</span><div class="">That's a great way to cause confusion.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">if condition {<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt();<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">}<br class=""></span></div>
<div class="">&nbsp;</div>
<div class="">but it would be confusing because semicolons are almost never used in Swift.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">An alternative that would work today is just relying on assignment returning Void, so you can write<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">if condition {<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">&nbsp; &nbsp; _ = funcWithSideEffectsThatReturnsInt()<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">}<br class=""></span></div>
<div class="">&nbsp;</div>
<div class="">but that looks kind of weird and would probably also be confusing. Better than the semicolon rule I think, but still not great.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">if condition {<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt()<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">} else {<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">&nbsp; &nbsp; funcWithSideEffectsThatReturnsString()<br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">}</span></div>
<div class="">&nbsp;</div>
<div class="">and it would be fine but writing<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">let foo = if condition {</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class=""><br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">&nbsp; &nbsp; funcWithSideEffectsThatReturnsInt()</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class=""><br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">} else {</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class=""><br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">&nbsp; &nbsp;&nbsp;funcWithSideEffectsThatReturnsString()</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class=""><br class=""></span></div>
<div class=""><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif" class="">}</span><br class=""></div>
<div class="">&nbsp;</div>
<div class="">would fail with a type error.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I suspect that this is the right approach, but it does involve a bit of magic.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">-Kevin</div>

<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qwYxTkgxSf6q1m03-2Bfxemp0tcToGEB9v2WUGD19yU-2FFr6p-2FyMpbOYaxUiM-2F-2B-2Benp-2FGCqd3qhS0O-2F6n1PN9hSdqyEke4PicltKRWBiJ4euSoSQ5zBiODF4WjmD3gqKM-2BZAy55irYRzWqZsMq-2BgnsA3ZIf5vCaSqhJ7qjG-2F6wzLxdoaSUr8Q6wyiaTuQiu6aYGzE-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
</div>


<br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=WzenSAN-2FiuX4NjI8OMkm0C8OpI7loozbOyGjfevETZvw7GzXo54W1wP-2FpPxIU3IhcjIoDyRs0o4-2FzET4yYVqQcXpDS9tbww0k3o606lWPX-2BXxb1JAn5iuMLjvz7ZcYMz9NR9GmmfaRNeFhkB6fY3973qzipNYobRxhpszFs7us54aJJ6V-2BVuVjjn0nhMPQxfSkaW-2BnJnSousDKd3NLP17Kbrm4ZMJ-2FmHQmOpxWk-2Bqdg-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>