Last active
March 14, 2023 08:30
-
-
Save reddavis/73637c91b7ea1290219857076cc7aacb to your computer and use it in GitHub Desktop.
This file contains 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 ||= | |
func ||=<T>(lhs: inout T?, rhs: T) { | |
guard lhs == nil else { return } | |
lhs = rhs | |
} |
This file contains 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
import XCTest | |
final class OrEqualsTests: XCTestCase { | |
func test_orEquals() { | |
var int: Int? | |
int ||= 1 | |
XCTAssertEqual(int, 1) | |
int = 2 | |
int ||= 3 | |
XCTAssertEqual(int, 2) | |
var dictionary: [Int : Int] = [1 : 1] | |
dictionary[2] ||= 2 | |
XCTAssertEqual(dictionary[2], 2) | |
dictionary[1] ||= 5 | |
XCTAssertEqual(dictionary[1], 1) | |
XCTAssertEqual(dictionary, [1 : 1, 2 : 2]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment