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

Jacob Williams ponyboy47 at gmail.com
Tue Jun 27 12:43:36 CDT 2017


+1 to Adrians implementation. While I like the sugar of both, I don’t think the operator should necessarily always cause a fatalError. Maybe we could use both the !! For some kind of fatal error and Adrians !? To just use @noreturn to leave the current function.

> On Jun 27, 2017, at 11:29 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:
> 
> +1 I had a slightly different implementation.
> 
> https://gist.github.com/DevAndArtist/dad641ee833e60b02fd1db2dbb488c6a <https://gist.github.com/DevAndArtist/dad641ee833e60b02fd1db2dbb488c6a>
> //
> // UnwrapOrTrap.swift
> //
> infix operator ?! : NilCoalescingPrecedence
> 
> /// Performs a nil-coalescing operation, returning the wrapped value of an
> /// `Optional` instance or uses the rhs function to stop the program.
> ///
> /// - Parameters:
> ///   - optional: An optional value.
> ///   - noreturn: A function to stop the programm.
> func ?!<T>(optional: T?, noreturn: @autoclosure () -> Never) -> T {
>    switch optional {
>    case .some(let value):
>       return value
>    case .none:
>       noreturn()
>    }
> }
> 
> 
> 
> -- 
> Adrian Zubarev
> Sent with Airmail
> 
> Am 27. Juni 2017 um 19:25:15, Erica Sadun via swift-evolution (swift-evolution at swift.org <mailto:swift-evolution at swift.org>) schrieb:
> 
>> 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 <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
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

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


More information about the swift-evolution mailing list