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

Yuta Koshizawa koher at koherent.org
Wed Jun 28 04:52:35 CDT 2017


Hi, I think it is an orthogonal issue if we need a new operator. It is
also possible to introduce an infix `!` for it.

I am sure that we do not need to avoid `precondition` as long as we
use it appropriately. It is useful to realize consistent behavior with
`Array`'s `subscript`, forced unwrapping `!`, `&+` and so on. In this
context, `precondition` does not mean a general word "precondition"
but the `precondition` function in the Swift standard library, which
is removed when -Ounchecked.

However I noticed the possibility that -Ounchecked removed the `nil`
check by `??` in the following code using `preconditionFailure` as a
clue of the optimization.

    let bar: Foo = optionalFoo ?? preconditionFailure("Reason why you
are sure that `optionalFoo` is not nil")

So now I think using `?? preconditionFailure(message)` with `Never` as
a bottom type is preferable to `!!` even in a standpoint of
-Ounckecked.

--
Yuta


2017-06-28 15:52 GMT+09:00 Elviro Rocca <retired.hunter.djura at gmail.com>:
> Yep, "logic" failures are not supposed to be there if not for avoiding
> checks that could worsen performance in pieces of code that should run as
> fast as possible. I like Adrian's solution because it doesn't add a new
> operator to the standard library, something that could mean one more
> encouragement to force unwrap things.
>
> About preconditions, Cocoa with Love's Matt Gallagher wrote an article some
> months ago on partial functions, why you should avoid them and how to do so:
> it's a very interesting read
> http://www.cocoawithlove.com/blog/2016/01/25/partial-functions-part-one-avoidance.html
>
>
> Elviro
>
> Il giorno 28 giu 2017, alle ore 07:02, Yuta Koshizawa via swift-evolution
> <swift-evolution at swift.org> ha scritto:
>
> ...
>
> Logic failures are intended to be handled by fixing the code. It means
> checks of logic failures can be removed if the code is tested enough.
> Actually checks of logic failures for various operations, `!`,
> `array[i]`, `&+` and so on, are designed and implemented to be removed
> when we use `-Ounchecked`. It is useful for heavy computation like
> image processing and machine learning in which overhead of those
> checks is not permissible.
>
> So I think checks for `!!` should be removed with `-Ounchecked` as well.
>
> --
> Yuta
>
>


More information about the swift-evolution mailing list