[swift-evolution] !? operator for ternary conditional unwrapping

Adrian Zubarev adrian.zubarev at devandartist.com
Wed Feb 8 13:13:26 CST 2017


For more information about optional chaining read this docs: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html



-- 
Adrian Zubarev
Sent with Airmail

Am 8. Februar 2017 um 20:05:31, Adrian Zubarev (adrian.zubarev at devandartist.com) schrieb:

Swift does already have a great solution in such a scenario.

func handler() -> Result? {
  return callback?.result()
}
Does the trick. ;)



-- 
Adrian Zubarev
Sent with Airmail

Am 8. Februar 2017 um 20:02:38, Maxim Veksler (maxim at vekslers.org) schrieb:

For example, assume that we're dealing with a callback of some form.

let callback: CallbackProtocol? = StructExampleCallback()

You could write inside you handler function:

func handler() -> Result? {
  return callback ?! callback.result() : nil
}

Which would return either nil, or the callback result. I don't think you can do it with ?? but then again guard is a valid solution for this specific use case.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170208/8b4473dd/attachment.html>


More information about the swift-evolution mailing list