Skip to content

Instantly share code, notes, and snippets.

@cjnevin
Last active November 1, 2022 22:03
Show Gist options
  • Save cjnevin/8ace1f04d787a9f2318e69ecc4bcaa71 to your computer and use it in GitHub Desktop.
Save cjnevin/8ace1f04d787a9f2318e69ecc4bcaa71 to your computer and use it in GitHub Desktop.
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