<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Perhaps it should provide a Fix-It, but I don't see it as an an issue, more as an opportunity to understand the pattern matching behind it :)</div><div><br>On 29 Jun 2016, at 08:59, Charlie Monroe &lt;<a href="mailto:charlie@charliemonroe.net">charlie@charliemonroe.net</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div class="">Yes, you get a warning. But I don't see why it should - when you're matching against an optional, it's evident that you mean .some("Hello")... This should be automatically inferred.</div><div class=""><br class=""></div><div class="">I find this a confusing part of the switch statement where&nbsp;</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> obj: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Hello"</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">obj</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">case</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> str </span><span style="font-variant-ligatures: no-common-ligatures" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="color: rgb(61, 29, 129); font-variant-ligatures: no-common-ligatures;" class="">print</span><span style="color: rgb(0, 0, 0); font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="color: rgb(209, 47, 27); font-variant-ligatures: no-common-ligatures;" class="">"Hey, found String!"</span><span style="color: rgb(0, 0, 0); font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">default</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">&nbsp; &nbsp; print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"No find."</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class="">will print "Hey found String", but it won't allow you to match against a string literal. This is one of the first things that I found confusing when I first learned Swift.</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 29, 2016, at 8:49 AM, David Hart &lt;<a href="mailto:david@hartbit.com" class="">david@hartbit.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">I don't see it as a bug. The if statement calls an equality function which promotes the lhs to an Optional (or that accepts a non optional as lhs - can't remember) but the switch statement does pattern matching. Does the the compiler warn you when pattern matching an optional with a non-optional case?</div><div class=""><br class="">On 29 Jun 2016, at 07:16, Charlie Monroe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class="">I nevertheless think that this is a bug and should be addressed. There is no reason where<div class=""><br class=""></div><div class="">if stringOptional == stringNonOptional { ... }</div><div class=""><br class=""></div><div class="">works, but pretty much the same construct doesn't work in the switch-case. It should be perhaps solved via a bugreport at <a href="http://bugs.swift.org/" class="">http://bugs.swift.org</a>&nbsp;and via evolution, though...<br class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 29, 2016, at 12:31 AM, Nevin Brackett-Rozinsky 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=""><span style="font-size:12.8px" class="">I just got home and tested. The answer is yes, `case "text"?` does work.</span><div style="font-size:12.8px" class=""><font face="monospace, monospace" class=""><br class=""></font></div><div style="font-size:12.8px" class=""><div class=""><font face="monospace, monospace" class="">let optStr : String? = "text"</font></div><div class=""><font face="monospace, monospace" class="">switch optStr {</font></div><div class=""><font face="monospace, monospace" class="">case nil &nbsp; &nbsp; : print("Nil")</font></div><div class=""><font face="monospace, monospace" class="">case "text"? : print("Success")</font></div><div class=""><font face="monospace, monospace" class="">default &nbsp; &nbsp; &nbsp;: print("Default")</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class="">// Prints `</font><span style="font-family:monospace,monospace" class="">Success`</span></div></div><div class="" style="font-size:12.8px"><div id=":2zn" class="" tabindex="0"><img class="" src="https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif" style=""></div></div><span class="" style="font-size:12.8px"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Nevin</div></font></span><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jun 28, 2016 at 12:27 PM, Nevin Brackett-Rozinsky <span dir="ltr" class="">&lt;<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank" class="">nevin.brackettrozinsky@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Does `case "text"?` work?<div class="HOEnZb"><div class="h5"><div class=""><br class=""><br class="">On Tuesday, June 28, 2016, Kevin Nattinger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class=""><div class="">Case .none:</div><div class="">Case .some("string"):</div><div class=""><br class=""></div><div class=""><br class="">On Jun 28, 2016, at 06:40, Lucas Jordan via swift-evolution &lt;<a class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">Forgive me if this was/is discussed already, I am new to the process here....<div class=""><br class=""></div><div class="">(code is attached as a playground too)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Sometimes when I am working with a String? nil can be a reasonable value, and what I want to do is something like the following:</div><div class=""><br class=""></div><div class=""><p class=""><span class="">import</span><span class=""> UIKit</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">var</span><span class=""> str:</span><span class="">String</span><span class="">? = </span><span class="">"Hello, playground"</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">switch</span><span class=""> </span><span class="">str</span><span class="">{</span></p><p class=""><span class="">case</span><span class=""> </span><span class="">nil</span><span class="">:</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">print</span><span class="">(</span><span class="">"Nil!"</span><span class="">)</span></p><p class=""><font color="#ff9900" class=""><span class="">case</span><span class=""> </span><span class="">"Hello, playground"</span><span class="">: &nbsp;</span><span class="">//it would be super nice if this worked.</span></font></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">print</span><span class="">(</span><span class="">"Match"</span><span class="">)</span></p><p class=""><span class="">default</span><span class="">:</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">print</span><span class="">(</span><span class="">"Some other non nil value?"</span><span class="">)</span></p><p class=""><span class="">}</span></p></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">But it does not work, the orange &nbsp;text is a compile time error, "Expression pattern of type 'String' cannot match value of type 'String?'. I realize that this can be replaced with a let statement (<span class="">case</span><span class=""> </span><span class="">let</span><span class=""> s </span><span class="">where</span><span class=""> s == </span><span class="">"Hello, playground"</span><span class="">:</span>), but that is verbose.&nbsp;</div><div class=""><br class=""></div><div class="">Seems like the compiler could be OK with the orange text, since it is clearly not nil.</div><div class=""><br class=""></div><div class="">Thoughts?</div><div class=""><br class=""></div><div class="">-Lucas</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div>







<div class=""><br class=""></div></div>
</div></blockquote><blockquote type="cite" class=""><div class="">&lt;NilInSwitchStatements.playground.zip&gt;</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></blockquote></div>
</div></div></blockquote></div><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></div></blockquote></body></html>