[swift-users] Binary Operator '??' cannot be applied to operands of type 'T?' and 'Never'

Adrian Zubarev adrian.zubarev at devandartist.com
Thu Oct 6 02:21:35 CDT 2016


I’ve build a custom operator for this: https://gist.github.com/DevAndArtist/dad641ee833e60b02fd1db2dbb488c6a

infix operator ?! : NilCoalescingPrecedence

func ?!<T>(optional: T?, noreturn: @autoclosure () -> Never) -> T {
   switch optional {
   case .some(let value):
      return value
   case .none:
      noreturn()
   }
}

// Usage
let test: Int? = nil

test ?! fatalError("Message")


-- 
Adrian Zubarev
Sent with Airmail

Am 6. Oktober 2016 um 05:35:53, Jonathan Hull via swift-users (swift-users at swift.org) schrieb:

I used to be able to use ?? with fatalError(“Message”) to trap with a custom message in case of nil. This no longer works in Swift 3 (I think because of the update to never type)
_______________________________________________
swift-users mailing list
swift-users at swift.org
https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161006/8874f485/attachment.html>


More information about the swift-users mailing list