[swift-evolution] Optionals and nil in Switch statement
Lucas Jordan
lucasjordan at gmail.com
Tue Jun 28 08:40:27 CDT 2016
Forgive me if this was/is discussed already, I am new to the process
here....
(code is attached as a playground too)
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:
import UIKit
var str:String? = "Hello, playground"
switch str{
case nil:
print("Nil!")
case "Hello, playground": //it would be super nice if this worked.
print("Match")
default:
print("Some other non nil value?")
}
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.
Seems like the compiler could be OK with the orange text, since it is
clearly not nil.
Thoughts?
-Lucas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160628/9d6cfd01/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NilInSwitchStatements.playground.zip
Type: application/zip
Size: 7104 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160628/9d6cfd01/attachment-0001.zip>
More information about the swift-evolution
mailing list