Skip to content

Instantly share code, notes, and snippets.

@LucianoPAlmeida
Last active April 24, 2020 00:35
Show Gist options
  • Save LucianoPAlmeida/5e91b5c413295d2f9f59a82f8de8652d to your computer and use it in GitHub Desktop.
Save LucianoPAlmeida/5e91b5c413295d2f9f59a82f8de8652d to your computer and use it in GitHub Desktop.
KeyPath<Root, Value> example
struct Player: CustomDebugStringConvertible {
var number: Int
var name: String
var debugDescription: String { "Player {number=\(number), name= \(name)}" }
}
func keyPaths<R, T>(_ base: R, kp: KeyPath<R, T>) {
let value = base[keyPath: kp]
print("kp value: \(value)")
}
var kobe = Player(number: 8, name: "Bryant")
keyPaths(kobe, kp: \.number) // kp value: 24
keyPaths(kobe, kp: \.name) // kp value: Bryant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment