[swift-evolution] [Pitch] Introducing the "Unwrap or Die" operator to the standard library

Ben Cohen ben_cohen at apple.com
Wed Jun 28 10:35:27 CDT 2017


> On Jun 28, 2017, at 6:03 AM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> In general, does everyone prefer `?? () -> Never` or `!! () -> Never`? I can argue both ways, with the goal in reading code as "unwrap or die".
> 


Personally, I strongly prefer `foo !! "explanation"` over `foo ?? fatalError()`. I don’t think adding another operator for what is a very common need is a concern, and is easier to read at a glance than the ??/fatalError combo.  There is a bit of cognitive dissonance between ??, a normally-fluffy nonthreatening defaulting operator, and fatalError, a scary beware operator. `!!` OTOH matches `!` in clearly flagging the unwrap as unsafe and that your program will exit. I also think it would be easier to explain `!!` to beginners as part of a wider story explaining optionals in general, unwraps, what `nil` means and what `!` means etc. Whereas `foo ?? Never` requires a lot of heavy lifting to understand how/why it works – we might all understand it, but that understanding is built on a lot of foundational understanding.

I also think a `Never` type would be very useful. Just that for this particular case, `!!` is still worth adding too.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170628/16a7290d/attachment.html>


More information about the swift-evolution mailing list