Swift: the Unused Optional Value Problem
Here is a scenario – you need to do something in your code based on whether an optional value exists or not, but the thing you need to do does not need to actually use the optional value.
[…]
This immediately stood out as “wrong” to me. By now, I’m very well trained to unwrap optionals all the time, so having a
!= nil
just feels wrong in Swift. Also, I don’t like the!=
here – I had to do a double take to realize that this code executes if something is actually there, so to me, it’s also not as readable. Of course, this code works and is completely “correct” for the situation, if that’s what you prefer.
It just doesn’t feel semantically correct. The optional is not
nil
, it specifically has a value ofNone
. I’ve never liked this syntax; I’ve talked about it before - thankfully thebool
comparison has been lost, but thenil
check still sits wrong with me.