[swift-evolution] Brainstorming: Optional sugar inferred map

Paul Ossenbruggen possen at gmail.com
Tue Feb 16 09:14:43 CST 2016


Yeah, sorry did that kind of quick. Just trying to express the idea, maybe my implementation left something to be desired. I wanted the result not the optional. 


> On Feb 15, 2016, at 10:35 PM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
> 
> 
> Am 16.02.2016 um 05:52 schrieb Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
> 
>> I guess if auto closure could take a parameter you could do something like this:
>> 
>> @warn_unused_result
>> public func ??<T>(optional: T?, @autoclosure defaultValue: (result : T) throws -> T) rethrows -> T {
>>     return optional != nil ? optional! : try defaultValue(result: optional!)
>> }
> 
> This will always throw an exception as you are force unwrapping the optional in both branches of the ternary, i.e. also in the branch which is used when it is nil.
> 
> -Thorsten 

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


More information about the swift-evolution mailing list