Swift: coalescing assignment (??=) operator

Originator:jtbandes
Number:rdar://22469833 Date Originated:8/27/2015
Status:Duplicate/19833281 Resolved:
Product:Developer Tools Product Version:Xcode 7.0 beta 6 (7A192o)
Classification:Enhancement Reproducible:
 
It would be nice to have a coalescing assignment operator:


infix operator ??= { associativity right precedence 90 }
public func ??=<T>(inout lhs: T?, @autoclosure rhs: Void -> T?)
{
    lhs = lhs ?? rhs()
}



Such that


    var x = functionThatReturnsOptionalInt()
    x ??= 42

    self.myProperty ??= MyObject(...)


is equivalent to

    let x = functionThatReturnsOptionalInt() ?? 42

    if self.myProperty == nil { self.myProperty ??= MyObject(...) }

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!