Skip to content

Instantly share code, notes, and snippets.

@giginet
Created August 2, 2016 15:34
Show Gist options
  • Save giginet/ab3c800087fe0f42af1dfb5a2c0b4dae to your computer and use it in GitHub Desktop.
Save giginet/ab3c800087fe0f42af1dfb5a2c0b4dae to your computer and use it in GitHub Desktop.
||= operator in Swift
infix operator ||= { associativity left precedence 150 }
public func ||= <T>(lhs: inout T?, rhs: T?) {
if lhs == nil { lhs = rhs }
}
var a: Int? = nil
a ||= 10
print(a)
var b: Int? = 20
b ||= 10
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment