SE-0217 - The "Unwrap or Die" operator

In previous phases of this proposal, I've favored the ?? + bottom Never alternative, because it gives us several different constructs like this. However, my thinking has evolved and I now support this proposal.

Instead of writing a lot of boring words about why this proposal is a good idea, I've prepared a visual aid:

Basically, I think that !! guides people into better use of force-unwraps. Merely asking people to "explain why this cannot be nil" may help people learn when a force-unwrap is appropriate and when they should do something else instead.

And that teaching advantage is one that ?? fatalError() can't match. This is why my opinion on !! changed: Even if we supported Never values on the right side of ??—and I still think we should!—the !! operator would still be a good idea purely as a shorthand for a best practice we very strongly recommend.

I have a couple of minor concerns:

  • I'm worried that infix !! may look confusingly similar to infix ||. One way to address this would be to change the operator to a single !; that would still be confusable with the | operator, but that one is used much less often. The parallel to ?? may be more important, though.

  • The inability to point to the location of the error correctly is very disappointing. It's something we can try to improve later, though—and I can think of at least two ways to do it besides the proposal's suggestion, one much deeper and the other much hackier.

But despite these small issues, I think this proposal is a great idea and, if it's accepted, I expect to use it pretty often.

9 Likes