Created
April 26, 2022 16:26
-
-
Save DanielCardonaRojas/a2d77f65e7bfc20a7c0bb76b72a9dea1 to your computer and use it in GitHub Desktop.
Optional assignment operator #swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator ??= : AdditionPrecedence | |
extension Optional { | |
static func ??= (_ rhs: inout Self, _ lhs: Self) { | |
if rhs == nil { | |
rhs = lhs | |
} | |
} | |
} | |
var a: Int? = nil | |
var c: Int? = 9 | |
let b = 13 | |
a ??= b | |
c ??= b | |
print(a) | |
print(c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment