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

Amir Michail a.michail at me.com
Sun Jan 31 08:26:25 CST 2016


> On Jan 31, 2016, at 4:03 AM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
> 
> 
>> 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.

?? can stay as is and something else could be specialized to a boolean context.

> 
> 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