Skip to content

Instantly share code, notes, and snippets.

@oconnelltoby
Created March 25, 2021 00:01
Show Gist options
  • Save oconnelltoby/dd5d7fa105803c1ca39c52fe6130af8f to your computer and use it in GitHub Desktop.
Save oconnelltoby/dd5d7fa105803c1ca39c52fe6130af8f to your computer and use it in GitHub Desktop.
@dynamicMemberCallable with KeyPath
@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