[swift-evolution] [Pitch] Introducing the "Unwrap or Die" operator to the standard library
Yuta Koshizawa
koher at koherent.org
Wed Jun 28 00:02:33 CDT 2017
I like it, but I think the implementation can be improved like below.
public static func !!(optional: Optional, errorMessage:
@autoclosure () -> String) -> Wrapped {
precondition(optional != nil, errorMessage())
return optional!
}
Failures of forced unwrapping are "logic failures" and I think it is
same about `!!`.
https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#logic-failures
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
2017-06-28 2:16 GMT+09:00 Erica Sadun via swift-evolution
<swift-evolution at swift.org>:
> Using an operator to provide feedback on the context of a failed unwrap has
> become a commonly implemented approach in the Swift developer Community.
> What are your thoughts about adopting this widely-used operator into the
> standard library?
>
> guard !lastItem.isEmpty else { return }
> let lastItem = array.last !! "Array must be non-empty"
>
> Details here:
> https://gist.github.com/erica/423e4b1c63b95c4c90338cdff4939a9b
>
> Thank you for your thoughtful feedback, -- E
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
More information about the swift-evolution
mailing list