# Auto protocol conformance if Key Path Member Lookup meets protocol requirement

**URL:** <https://forums.swift.org/t/auto-protocol-conformance-if-key-path-member-lookup-meets-protocol-requirement/52708>\
**Category:** Discussion\
**Tags:** generics\
**Created:** [October 9, 2021, 6:33pm UTC](https://forums.swift.org/t/auto-protocol-conformance-if-key-path-member-lookup-meets-protocol-requirement/52708 "2021-10-09T18:33:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim.Wang](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@Tim.Wang](https://forums.swift.org/u/Tim.Wang)\
**Post date:** [October 9, 2021, 6:33pm UTC](https://forums.swift.org/t/auto-protocol-conformance-if-key-path-member-lookup-meets-protocol-requirement/52708/1 "2021-10-09T18:33:22Z")

</div>

With [Key Path Member Lookup](https://github.com/apple/swift-evolution/blob/main/proposals/0252-keypath-dynamic-member-lookup.md), the compiler knows that certain properties could be provided by this subscript function with known types. We can even see this types of properties in code completion.

However, the compiler does not treat this as a proper protocol conformance. Please refer the code below:

```swift
protocol P {
    var property: Bool { get }
}

struct MyValue {
    let property = "Sample property"
}

@dynamicMemberLookup
struct S: P {
    let value = MyValue()
    subscript<T>(dynamicMember member: KeyPath<MyValue, T>) -> T {
        value[keyPath: member]
    }
}

```

The compiler will complain `Type 'S' does not conform to protocol 'P'`. Is it possible to let compiler add protocol conformance automatically for this case and treat it as valid code?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 9, 2021, 7:40pm UTC](https://forums.swift.org/t/auto-protocol-conformance-if-key-path-member-lookup-meets-protocol-requirement/52708/2 "2021-10-09T19:40:37Z")

</div>

> [@Introduce “Dynamic Member” fulfillment of Protocols](https://forums.swift.org/t/introduce-dynamic-member-fulfillment-of-protocols/13205/6):
>
> FWIW, just explaining thought process here: This was suggested and I specifically considered this and pushed back on this during the review process. The entire point of the dynamic member lookup feature is to allow unbound syntactic extension of a member lookup in the case when the author of a type cannot enumerate all of the members that a user might want to use statically. In the case of protocol conformance, a protocol does have a specific static list of members that need to be satisfied. …

---

<div class="post-metadata">

**Author:** ![Tim.Wang](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@Tim.Wang](https://forums.swift.org/u/Tim.Wang)\
**Post date:** [October 9, 2021, 8:32pm UTC](https://forums.swift.org/t/auto-protocol-conformance-if-key-path-member-lookup-meets-protocol-requirement/52708/3 "2021-10-09T20:32:37Z")

</div>

Thanks for pointing out the original discussion thread. Conceptually they indeed come from different world. But if it happens that we have to have these two in the code, it seems redundant though.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [October 10, 2021, 3:38am UTC](https://forums.swift.org/t/auto-protocol-conformance-if-key-path-member-lookup-meets-protocol-requirement/52708/4 "2021-10-10T03:38:31Z")

</div>

As mentioned at the bottom of that thread, it seems likely we'd want to reconsider that point now that there's `KeyPath`-based `dynamicMemberLookup`, which didn't exist when Chris made his original post. Given that the `KeyPath` version allows compile time verification of the protocol requirements, it seems at least some of the original objections may no longer apply. It does, however, raise ergonomic issues around how to tell uses that the property requirements of a protocol can be met automatically by a `KeyPath` while functional requirements must still be bridged manually.
