<div dir="ltr"><span style="font-size:12.8px">I just got home and tested. The answer is yes, `case "text"?` does work.</span><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8px"><div><font face="monospace, monospace">let optStr : String? = "text"</font></div><div><font face="monospace, monospace">switch optStr {</font></div><div><font face="monospace, monospace">case nil : print("Nil")</font></div><div><font face="monospace, monospace">case "text"? : print("Success")</font></div><div><font face="monospace, monospace">default : print("Default")</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace">// Prints `</font><span style="font-family:monospace,monospace">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"><div><br></div><div>Nevin</div></font></span><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 28, 2016 at 12:27 PM, Nevin Brackett-Rozinsky <span dir="ltr"><<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.com</a>></span> wrote:<br><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><br><br>On Tuesday, June 28, 2016, Kevin Nattinger via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>Case .none:</div><div>Case .some("string"):</div><div><br></div><div><br>On Jun 28, 2016, at 06:40, Lucas Jordan via swift-evolution <<a>swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Forgive me if this was/is discussed already, I am new to the process here....<div><br></div><div>(code is attached as a playground too)</div><div><br></div><div><br></div><div><br></div><div>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><br></div><div>
<p><span>import</span><span> UIKit</span></p>
<p><span></span><br></p>
<p><span>var</span><span> str:</span><span>String</span><span>? = </span><span>"Hello, playground"</span></p>
<p><span></span><br></p>
<p><span>switch</span><span> </span><span>str</span><span>{</span></p>
<p><span>case</span><span> </span><span>nil</span><span>:</span></p>
<p><span> </span><span>print</span><span>(</span><span>"Nil!"</span><span>)</span></p>
<p><font color="#ff9900"><span>case</span><span> </span><span>"Hello, playground"</span><span>: </span><span>//it would be super nice if this worked.</span></font></p>
<p><span> </span><span>print</span><span>(</span><span>"Match"</span><span>)</span></p>
<p><span>default</span><span>:</span></p>
<p><span> </span><span>print</span><span>(</span><span>"Some other non nil value?"</span><span>)</span></p>
<p><span>}</span></p></div><div><br></div><div><br></div><div>But it does not work, the orange 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>case</span><span> </span><span>let</span><span> s </span><span>where</span><span> s == </span><span>"Hello, playground"</span><span>:</span>), but that is verbose. </div><div><br></div><div>Seems like the compiler could be OK with the orange text, since it is clearly not nil.</div><div><br></div><div>Thoughts?</div><div><br></div><div>-Lucas</div><div><br></div><div><br></div><div><br></div><div><br></div>
<div><br></div></div>
</div></blockquote><blockquote type="cite"><div><NilInSwitchStatements.playground.zip></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a>swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></blockquote></div>
</div></div></blockquote></div><br></div></div>