NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Chris Lattner explains optionals

Recorded – watch the full episode on YouTube.

Swift learners usually have a great time with variables, constants, enums, loops, and more, but then they come to optionals and so many people struggle with them. Chris, you invented them, so how would you explain optionals to them?

Chris Lattner: This is one of those cases where history works against us because particularly when you're launching Swift into a world filled with Objective-C familiar people. Objective-C does not work this way and you have pointers and people conflate the idea of pointers and the idea of nullability with optionals, which is how they get expressed when things get imported into Swift.

Unfortunately, that's absolutely the wrong way to think about it. As, Paul I think you know, optionals is about “is something there or is it missing”, right? So it really has nothing to do with pointers. Optionals have nothing to do with pointers from a conceptual level but, because of the way Objective-C worked and that kind of stuff and because everything was a pointer, it really got conflated.

"The place optionals come in naturally to me is when you get to dictionaries."

To a new learner, somebody who is learning programming from scratch, I think that there's in my mind at least a pretty clear way to explain this, going through your trajectory of learning, programming and learning new language from scratch, you have to confront things like variables, we have control flow statements, ifs, things like this, we have imperative execution. We have functions. At some point you get to we have arrays and then you start talking about this.

The place optionals come in naturally to me is when you get to dictionaries. You say, okay, well, we have this thing. It's a collection of strings to things, whatever you want to use for your example. And you say we can put a bunch of data in a dictionary, we could print it out. Oh, this makes sense. Oh, look, notice the keys and the values do not remain sorted. That's because how dictionaries work and they say, “let's get something out of a dictionary." But wait, what is that? It returns an optional thing.

"I think dictionaries are probably the first point in the trajectory of learning Swift, that they're both relatively easy to explain but then also they can make intuitive sense."

Well, what is optional? The problem is the thing you look up in the dictionary may not be there. So what that's saying is it's either returning the thing that you asked for or it's saying it's not there. So optionals are a way of saying when you ask a question, you may get the answer back or you may get a nope and so optionals.

I think dictionaries are probably the first point in the trajectory of learning Swift, that they're both relatively easy to explain but then also they can make intuitive sense and put somebody on the right track of thinking about them as the presence or absence of a value.

Now, when you talk about some of the syntactic sugar and stuff like that, that's another place where the history of Swift interfacing with Objective-C and C, I think made the value of having a lot of sugar like ?? and optional chaining and all these things really, really important. So just the prevalence of optionals and implicitly unwrapped options and all that complexity that came from the grungy reality of having it talk to C and Objective-C really made the syntactic sugar important. And that added a lot of complexity in the language.

"If you're talking about a new learner, you don't start with optional chaining. You start with if let and so if let is the thing you need to know, and that's enough."

If there were a world that was pure and beautiful and had no compatibility constraints, we probably would have put less of that sugar in and that would have been easier to explain. But again, if you're talking about a new learner, you don't start with optional chaining. You start with if let and so if let is the thing you need to know, and that's enough, right?

You can say it's like an if and it has this behavior and it lets you see what's inside the optional. If it's wrong, you go to the else and it's pretty concrete and pretty easy to explain. And then some time later you can explain optional chaining and things like that in terms of if let and the other simpler constructs.

Paul Hudson: I remember explaining it to my daughter when she was nine because she was asking about optionals because she uses playgrounds. My explanation was about presents, a boxed present and you can open the box and look inside and unwrap the present and look inside for the present. But if you had a present, from me, "Here you go, Sophie, this is to give to your friend for her birthday." You give it to your friend and you haven't checked inside there's a present there, it could be empty and I'm playing a prank on you. It's going to be a bad thing. And that's like force unwrapping the optional.

You want to check inside there first carefully before you pass it on. But it made sense. And she was like, “oh yeah, okay fine.” And she moves on to the next thing. It just took a bit of time to get over it.

I think a lot of folks get the idea of “maybe, maybe not” fairly quickly, but I remember the early days thinking, “when do I want question mark? When do I want exclamation mark?" Because you need both, at least in UIKit land.

Chris Lattner: Well, I'm laughing at you because I'm imagining Paul, the father, who his daughter understands the idea of like "I give you presents and sometimes there's nothing inside." See, what does it say about you and your relationship? Come on.

Paul Hudson: It's so bad!

This transcript was recorded as part of Swiftly Speaking. You can watch the full original episode on YouTube, or subscribe to the audio version on Apple Podcasts.

Listen on Apple Podcasts

Hacking with Swift is sponsored by Stream

SPONSORED Check out Stream's cross-platform open source chat SDK on GitHub! Write once and deploy your app with fully featured chat UI on iOS and macOS.

Go to GitHub

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.