Last active
November 1, 2022 22:03
-
-
Save cjnevin/8ace1f04d787a9f2318e69ecc4bcaa71 to your computer and use it in GitHub Desktop.
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
| struct User { | |
| struct Prefs { | |
| let email: Bool, sms: Bool | |
| } | |
| let name: String, prefs: Prefs | |
| } | |
| let user = User(name: "Test", prefs: Prefs(email: true, sms: false)) | |
| // Previously: | |
| assert(user.name.isEmpty) == true | |
| // Now, thanks to @dynamicMemberLookup: | |
| assert(user).name.isEmpty == true | |
| // When combined with closures, the real power starts to reveal itself: | |
| assert(user) { | |
| $0.name == "Test" | |
| $0.prefs.email == true | |
| $0.prefs.sms == false | |
| } | |
| // Deep Nesting | |
| assert(user) { | |
| $0.name == "Test" | |
| assert($0.prefs) { | |
| $0.email == true | |
| $0.sms == false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment