<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Then I knew it. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><p 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;color:rgb(186,45,162)">if</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> a: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">A</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">A</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">() { </span><span style="font-variant-ligatures:no-common-ligatures">// this is a warning as it treats like always true</span></p><p 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;color:rgb(0,0,0)"> </span><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)">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">"something"</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">) </span><span style="font-variant-ligatures:no-common-ligatures">// this line always runs</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">
</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">}</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">For </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><p 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;color:rgb(186,45,162)">if</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> a = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">A</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">() { </span><span style="font-variant-ligatures:no-common-ligatures">// show an error as the compiler thinks you are missing something.</span></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">}</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">For me, the first example, explicitly given the type of `a`, so it is a warning. The second example, is one step farther than the first one. And it is shown as an error.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Each one of them is correct by itself. But putting together, there is a self-contradictory feeling.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhao Xin</div><div class="gmail_default" style="font-family:georgia,serif"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 4, 2017 at 6:00 PM, Martin R <span dir="ltr"><<a href="mailto:martinr448@gmail.com" target="_blank">martinr448@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I don’t think that explains it (or perhaps I did not understand your response correctly).<br>
<br>
Here is the same issue with a custom type (which does not have a failable initializer):<br>
<br>
struct A { }<br>
<br>
if let a = A() { }<br>
// error: initializer for conditional binding must have Optional type, not 'A'<br>
<br>
if let a: A = A() { }<br>
// warning: non-optional expression of type 'A' used in a check for optionals<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
> Am 02.06.2017 um 15:49 schrieb Zhao Xin <<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>>:<br>
><br>
> I think it did an unnecessary implicitly casting. For example,<br>
><br>
> let y = Int(exactly: 5)<br>
> print(type(of:y)) // Optional<Int><br>
><br>
> You code equals to<br>
><br>
> if let y:Int = Int(exactly: 5) { }<br>
><br>
> However, I don't know why it did that. Maybe because of type inferring?<br>
><br>
> Zhaoxin<br>
><br>
> On Fri, Jun 2, 2017 at 8:40 PM, Martin R via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
> This following code fails to compile (which is correct, as far as I can judge that):<br>
><br>
> if let x = 5 { }<br>
> // error: initializer for conditional binding must have Optional type, not 'Int'<br>
><br>
> But why is does it compile (with a warning) if an explicit type annotation is added?<br>
><br>
> if let y: Int = 5 { }<br>
> // warning: non-optional expression of type 'Int' used in a check for optionals<br>
><br>
> 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.<br>
><br>
> I am just curious and would like to understand if there is fundamental difference between those statements.<br>
><br>
> Regards, Martin<br>
><br>
><br>
><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>
><br>
<br>
</div></div></blockquote></div><br></div>