<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" 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:<div class=""><br class=""></div><div class=""><font face="Courier New" class="">public func ~=<T : Equatable>(a: T?, b: T?) -> 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="">}</font></div><div class=""><br class=""></div><div class="">For better or worse, this prevents you from using the `.some(x)` / `.none` version.</div><div class=""><br class=""></div><div class="">You could propose adding this to the standard library to the swift-evolution list, see how they react.</div><div class=""><br class=""><blockquote type="cite" class="">On Jun 28, 2016, at 9:52 AM, Lucas Jordan <<a href="mailto:lucasjordan@gmail.com" class="">lucasjordan@gmail.com</a>> 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 <<a href="mailto:swift@nattinger.net" class="">swift@nattinger.net</a>> wrote:<br class="">No<br class=""><br class=""> 7> switch str {<br class=""> 8. case "foo": print("case foo")<br class=""> 9. case .none: print("(nil)")<br class=""> 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=""> ^<br class=""> !<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 <nevin.brackettrozinsky@gmail.com> 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 <swift-evolution@swift.org> 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 <swift-evolution@swift.org> 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=""> print("Nil!")<br class=""><br class="">case "Hello, playground": //it would be super nice if this worked.<br class=""><br class=""> print("Match")<br class=""><br class="">default:<br class=""><br class=""> 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 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 (case let s where s == "Hello, playground":), but that is verbose. <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=""><NilInSwitchStatements.playground.zip><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=""></div></body></html>