Created
March 25, 2021 00:01
-
-
Save oconnelltoby/dd5d7fa105803c1ca39c52fe6130af8f to your computer and use it in GitHub Desktop.
@dynamicMemberCallable with KeyPath
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
@dynamicMemberLookup | |
protocol Passthrough { | |
associatedtype Previous | |
var previous: Previous { get } | |
subscript<T>(dynamicMember keyPath: KeyPath<Previous, T>) -> T { get } | |
} | |
extension Passthrough { | |
subscript<T>(dynamicMember keyPath: KeyPath<Previous, T>) -> T { | |
return previous[keyPath: keyPath] | |
} | |
} | |
struct ObjectOne: Passthrough { | |
var previous: Void | |
var valueOne: String | |
} | |
struct ObjectTwo: Passthrough { | |
var previous: ObjectOne | |
let valueTwo: String | |
} | |
struct ObjectThree: Passthrough { | |
let previous: ObjectTwo | |
let valueThree: String | |
} | |
let objectOne = ObjectOne(previous: (), valueOne: "One") | |
let objectTwo = ObjectTwo(previous: objectOne, valueTwo: "Two") | |
let objectThree = ObjectThree(previous: objectTwo, valueThree: "Three") | |
objectThree.valueOne | |
objectThree.valueTwo | |
objectThree.valueThree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment