<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Slight amendment for `let` case, which does evidently still require the explicit `.some`<div class=""><br class=""></div><div class=""><font face="Courier New" class="">switch str {<br class="">case "foo": print("foo")<br class="">case .some(let string): print(string)&nbsp;</font><span style="font-family: 'Courier New';" class="">// `case let .some(string)` also works</span></div><div class=""><font face="Courier New" class="">case nil: print("nil")<br class="">}</font></div><div class=""><br class=""><blockquote type="cite" class="">On Jun 28, 2016, at 11:10 AM, Kevin Nattinger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I’ve always thought it’s a bit odd, but that’s the way it is. FWIW, if you define `T? ~= T?` (switch uses `~=` under the hood), you can use that syntax:<br class=""><br class="">public func ~=&lt;T : Equatable&gt;(a: T?, b: T?) -&gt; Bool {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return a == b<br class="">}<br class=""><br class="">switch str {<br class="">case "foo": print("foo")<br class="">case "bar": print("bar")<br class="">case nil: print("nil")<br class="">default: print("other")<br class="">}<br class=""><br class="">For better or worse, this prevents you from using the `.some(x)` / `.none` version.<br class=""><br class="">You could propose adding this to the standard library to the swift-evolution list, see how they react.<br class=""><br class=""><blockquote type="cite" class="">On Jun 28, 2016, at 9:52 AM, Lucas Jordan &lt;<a href="mailto:lucasjordan@gmail.com" class="">lucasjordan@gmail.com</a>&gt; wrote:<br class=""><br class="">This is sort of weird right? because comparing nil to a non nil string is a reasonable thing to do:<br class=""><br class="">var nilString:String? = nil<br class=""><br class="">if nilString == "this always fails" {}<br class=""><br class="">is totally reasonable.<br class=""><br class="">On Tue, Jun 28, 2016 at 12:33 PM, Kevin Nattinger &lt;swift@nattinger.net&gt; wrote:<br class="">No<br class=""><br class="">&nbsp; 7&gt; switch str {<br class="">&nbsp; 8. case "foo": print("case foo")<br class="">&nbsp; 9. case .none: print("(nil)")<br class="">&nbsp;10. }<br class="">error: repl.swift:8:6: error: value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?<br class="">case "foo": print("case foo")<br class="">&nbsp; &nbsp; &nbsp;^<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !<br class="">Odd error, but at least it suggests it’s an issue with optionaity.<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jun 28, 2016, at 9:27 AM, Nevin Brackett-Rozinsky &lt;nevin.brackettrozinsky@gmail.com&gt; wrote:<br class=""><br class="">Does `case "text"?` work?<br class=""><br class=""><br class="">On Tuesday, June 28, 2016, Kevin Nattinger via swift-evolution &lt;swift-evolution@swift.org&gt; wrote:<br class="">Case .none:<br class="">Case .some("string"):<br class=""><br class=""><br class="">On Jun 28, 2016, at 06:40, Lucas Jordan via swift-evolution &lt;swift-evolution@swift.org&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">Forgive me if this was/is discussed already, I am new to the process here....<br class=""><br class="">(code is attached as a playground too)<br class=""><br class=""><br class=""><br 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:<br class=""><br class="">import UIKit<br class=""><br class=""><br class=""><br class="">var str:String? = "Hello, playground"<br class=""><br class=""><br class=""><br class="">switch str{<br class=""><br class="">case nil:<br class=""><br class="">&nbsp; &nbsp; print("Nil!")<br class=""><br class="">case "Hello, playground": &nbsp;//it would be super nice if this worked.<br class=""><br class="">&nbsp; &nbsp; print("Match")<br class=""><br class="">default:<br class=""><br class="">&nbsp; &nbsp; print("Some other non nil value?")<br class=""><br class="">}<br class=""><br class=""><br class=""><br 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&nbsp;that this can be replaced with a let statement (case let s where s == "Hello, playground":), but that is verbose.&nbsp;<br class=""><br class="">Seems like the compiler could be OK with the orange text, since it is clearly not nil.<br class=""><br class="">Thoughts?<br class=""><br class="">-Lucas<br class=""><br class=""><br class=""><br class=""><br class=""><br class=""></blockquote><blockquote type="cite" class="">&lt;NilInSwitchStatements.playground.zip&gt;<br class=""></blockquote><blockquote type="cite" class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote></blockquote><br class=""><br class=""></blockquote><br 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=""></blockquote><br class=""></div></body></html>