Binary Operator '??' cannot be applied to operands of type 'T?' and 'Never'


(Jon Hull) #1

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)


(Adrian Zubarev) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jacob Bandes-Storch) #3

Has there still not been a formal proposal for "Never as universal
subtype/bottom"? Someone please propose it :slight_smile:

Jacob

···

On Sat, Nov 12, 2016 at 12:20 AM, Adrian Zubarev via swift-users < swift-users@swift.org> wrote:

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users