SE-0217 - The "Unwrap or Die" operator

Character Description Postfix Infix
? The "safely execute" character ? - execute this expression if it's not nil. If it is nil, no-op. ?? - execute the left if it's not nil, but if it is nil, execute the right
! The "unsafely execute" character ! - execute this expression if it's not nil. If it is nil, crash. !! - execute the left if it's not nil, but if it is nil crash because of the right

Why not "?? () -> Never"

It would be incongruous with all existing precedence to make anything relating to a ? allow for crashing. ? is always a non-crashing operation and was created explicitly to avoid crashing.

On the other hand, ! always indicates the potential for crashing when it comes to optionals. The unary version, like unary ?, doesn't allow for fallbacks. Therefore, the binary version, like the binary ??, would crash because of whatever's on the right-hand side.

This is what I mean by "completeness". Using ?? to crash is incongruous with all other uses of ? in Swift. !! is the only logical spelling if we want to have a "crashing binary operator".

6 Likes