@dynamicMemberLookup get keyPath of chained properties

Hey everyone,

I'm playing around with @dynamicMemberLookup and noticed that the keyPath given to subscript(dynamicMember:) includes only the path to the first property if multiple fields are chained together.
E.g. proxy.first.second is transformed to proxy[dynamicMember: \.first].second but I would like it to be transformed to proxy[dynamicMember: \.first.second]

Code example
@dynamicMemberLookup
struct Proxy<Value> {
    var value: Value
    subscript<Field>(dynamicMember keyPath: KeyPath<Value, Field>) -> Field {
        get {
            let field = value[keyPath: keyPath]
            print(field)
            return field
        }
    }
}
struct A {
    var first: B
}
struct B {
    var second: Int
}

let proxy = Proxy(value: A(first: B(second: 1)))
_ = proxy.first.second

this prints

B(second: 1)

But I would like to capture the full keyPath and it should print

1

Is this somehow possible today with @dynamicMemberLookup?

Thanks
David

3 Likes