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)
I’ve build a custom operator for this: UnwrapOrTrap.swift · GitHub
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
Has there still not been a formal proposal for "Never as universal
subtype/bottom"? Someone please propose it :)
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/dad641ee833e60b02fd1db2dbb488c6ainfix 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? = niltest ?! fatalError("Message")
--
Adrian Zubarev
Sent with AirmailAm 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