Swift: The Unused Optional Value Problem

Yesterday, I had an interesting discussion involving Swift optionals with my co-workers. 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.

So here is the first try for this simple logic:

var someOptionalVar: String?

someOptionalVar = "something there"

if someOptionalVar != nil {
    // execute code that does NOT actually
    // use someOptionalVar
    println("hello")
}

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.

My immediate response was to use the unwrapping syntax in Swift instead:

var someOptionalVar: String?

someOptionalVar = "something there"

if let someVar = someOptionalVar {
    // execute code that does NOT actually
    // use someVar
    println("hello")
}

The problem here, as my coworkers pointed out, is that it’s creating a variable that’s not going to be used. And it’s equally, if not more confusing than the above solution, because reading this code, one immediately starts looking for the use of the unwrapped value, and it’s confusing that it’s not actually used (did the person forget to use it? should I use it?).

Luckily, @jsambuo pointed me to the perfect solution (IMHO) from this StackOverflow post, that I didn’t think of, but makes perfect sense.

var someOptionalVar: String?

someOptionalVar = "something there"

if let _ = someOptionalVar {
    // execute code that does NOT actually
    // use someOptionalVar
    println("hello")
}

This way, you still get to use the traditional Swift unwrapping optionals syntax, but it’s now very clear that you do not intend to use the unwrapped value.

Good reminder to use the _ in Swift for a lot more readable code, something that I often forget.

UPDATE: I now realize that this post reads as me saying that I “know” the right answer, and the right answer is the let _ = someOptionalVar option. The truth is, when my coworkers discussed this, we didn’t come up with the “right” solution, just preferences.

All I know is that when I saw the != nil in the Swift code, it took me a minute to process it and understand it, since it just wasn’t in the same Swift format that I’m now so used to for optionals. In any other language, I wouldn’t have given it a second thought.

And, rightly so, I’ve received comments that the != nil solution is way better and readable: see discussion on Twitter here, here, here, here, and here. In fact, Apple uses the != nil version in their own walk-through on Optionals.

The best way I can describe why the if let _ excited me so much is these tweets from @dandon and @jrbj:

Just like @dandon and @jrbj, I’ll have to try it out and see how I like it, and how my team feels about it in the actual codebase. Meanwhile, I’m keeping an open mind as I keep learning and discovering Swift. So I hope you read this and my other posts as a Swift exploration rather than a prescription.

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.