Okay, so I know the normal way to use optionals in Swift is via optional bindings to unwrap them, like so...
let stringA:String? = nil // (or "ABC")
if let unwrappedStringA = stringA
{
let x:String = unwrappedStringA
}
But I've also seen the following way using implicitly-unwrapped optionals, which I personally think looks cleaner as it not only doesn't require an extra variable, but it also 'reads' better and more like English (i.e. 'If this is not nil, then...') and readability is one of the core tenets of Swift (especially in the upcoming Swift 3.0).
let stringA:String! = nil // (or "ABC")
if stringA != nil
{
let x:String = stringA
}
However, in regards to the latter, Swift 'purists' refer to this as 'code smell' and insist it's 'Bad, bad, bad!!'... but they never explain why! So... why is it so bad?
Note: Yes, I know about Optional chaining and other such features which you can't use with implicitly-unwrapped optionals, and they are all really cool features, but I am specifically asking about the technical downside to testing implicitly-unwrapped optionals against nil vs optional binding.
What I'm hoping for are quantifiable, technical reasons why one is better than the other (i.e. compiler optimizations, better safety checking, performance, etc.) In other words, not just a 'Hey, because that's not the 'Swifty' way to do it!' Hope that makes sense.
Update
I actually just found a way to address one of my concerns (at least in a superficial way) which is having to create a new variable to hold the unwrapped one. The trick here is since the unwrapped variable is actually in a different scope than the optional itself, you can use the exact same name.
Further still, inside the brackets themselves is yet another scope which can also have a variable named stringA. This means you can now have three 'stringA' variables (but doesn't mean you should!)...
- The optional
- The unwrapped optional
- A new, local variable inside the brackets
Here's this crazy code showing this...
let stringA:String? = nil // (or "ABC") // First stringA variable
if let stringA = stringA
{
let x:String = stringA // <- This stringA is the unwrapped optional (the second stringA)
// Now we're just getting crazy (and smelly!)
let stringA = stringA + stringA // <- This is yet another stringA (third one!)
let y:String = stringA
}
Again, I am not condoning this!! I'm just showing some code that others may find interesting from an instructive perspective. I sure did!