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

Yuta Koshizawa koher at koherent.org
Thu Jun 29 21:35:22 CDT 2017


"In -Ounchecked builds, the optimizer may assume that this function is
never called. Failure to satisfy that assumption is a serious programming
error.”


Yes, and so the following `!!` can perform identically to `!` in
-Ounchecked builds when it is inlined.

public static func !!(optional: Optional, errorMessage: @autoclosure () ->
String) -> Wrapped { precondition(optional != nil, errorMessage()) return
optional! }

--
Yuta


2017-06-29 6:42 GMT+09:00 Jaden Geller <jaden.geller at gmail.com>:

>
> > On Jun 28, 2017, at 7:47 AM, Erica Sadun via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> >
> >> On Jun 28, 2017, at 3:52 AM, Yuta Koshizawa via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>
> >> 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.
> >
> >
> > How would the line run then? Would it simply act as a forced unwrapped
> under -Ounchecked?
>
> From the docs:
>
> "In -Ounchecked builds, the optimizer may assume that this function is
> never called. Failure to satisfy that assumption is a serious programming
> error.”
>
> Aka, v. bad things happen if the precondition does not hold.
>
> >
> > -- E
> >
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170630/3747e4d6/attachment.html>


More information about the swift-evolution mailing list