> On Aug 15, 2016, at 2:27 PM, Xiaodi Wu via swift-evolution 
> <swift-evolution@swift.org> wrote:
> 
> `let value = (x == nil) ? nil : foo.bar(x: x)` isn't so bad, is it? You could 
> even write a custom operator to sugar it.

It’s distasteful, due to the need to use the force-unwrap operator. In cases 
like this, I usually end up writing:

let value: Foo? = nil

if let x = x {
        value = foo.bar(x: x)
} else {
        value = nil
}

or:

let value: Foo? = {
        if let x = x {
                return foo.bar(x: x)
        } else {
                return nil
        }
}()

Both of which are unwieldy, but necessary to avoid the use of !.

I wouldn’t mind something like an overload on the ternary operator:

let value = x? ? foo.bar(x: x) : nil

in which a ? after the ternary condition indicates that it is an optional to be 
unwrapped for the positive condition.

Charles

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

Reply via email to