[swift-evolution] Proposal: Replace ?? by an (optional) argument to ?

Thorsten Seitz tseitz42 at icloud.com
Sun Jan 31 03:03:06 CST 2016


> Am 30.01.2016 um 18:27 schrieb Amir Michail <a.michail at me.com>:
> 
> not specialized to a boolean context (i.e, it is too general for most cases)

Why should ?? be specialized to a boolean context? There are lots of cases where you might want to use a default value for a missing optional, e.g.

label.text = document.name ?? "Unnamed document"

As your use case seems to be making "isEmpty" return true on missing optionals you can easily define the following

extension Optional {
    var isEmpty: Bool { 
        return self == nil || isEmpty
    }
}

if x.isEmpty { ... } // x is an optional

-Thorsten 

-Thorsten 


More information about the swift-evolution mailing list