<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I think it did an unnecessary implicitly casting. For example,</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> y = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">(exactly: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">5</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)">print</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(type(of:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">y</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)) </span><span style="font-variant-ligatures:no-common-ligatures">// Optional&lt;Int&gt;</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><br></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">You code equals to</font></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> y:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">(exactly: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">5</span><span style="font-variant-ligatures:no-common-ligatures">) { }</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><br></p></div><div class="gmail_default" style="font-family:georgia,serif">However, I don&#39;t know why it did that. Maybe because of type inferring?</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 2, 2017 at 8:40 PM, Martin R via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">This following code fails to compile (which is correct, as far as I can judge that):<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">   if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> x = </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">5</span><span style="font-variant-ligatures:no-common-ligatures"> { }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   <font color="#008400">// error: initializer for conditional binding must have Optional type, not &#39;Int&#39;</font></span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>But why is does it compile (with a warning) if an explicit type annotation is added?</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">   if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> y: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Int</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">5</span><span style="font-variant-ligatures:no-common-ligatures"> { }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures">   // warning: non-optional expression of type &#39;Int&#39; used in a check for optionals</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>Tested with Xcode 8.3.2 and both the build-in Swift 3.1 toolchain and the Swift 4.0 snapshot from May 25, 2017.</div><div><br></div><div>I am just curious and would like to understand if there is fundamental difference between those statements.</div><div><br></div><div>Regards, Martin</div><div><br></div><div><br></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>