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

Max Moiseev moiseev at apple.com
Wed Jun 28 13:00:53 CDT 2017


One argument against `?? () -> Never` is that `func f() -> Never { while true {} }` will also work.

Max

> On Jun 28, 2017, at 8:35 AM, Ben Cohen <ben_cohen at apple.com> wrote:
> 
> 
>> On Jun 28, 2017, at 6:03 AM, Erica Sadun via swift-evolution <swift-evolution at swift.org <mailto: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/c8555c3b/attachment.html>


More information about the swift-evolution mailing list