# @dynamicMemberLookup keyPaths not working with generic classes?

**URL:** <https://forums.swift.org/t/dynamicmemberlookup-keypaths-not-working-with-generic-classes/31688>\
**Category:** Using Swift\
**Tags:** generics, keypaths, dynamicmemberlookup\
**Created:** [December 14, 2019, 11:23pm UTC](https://forums.swift.org/t/dynamicmemberlookup-keypaths-not-working-with-generic-classes/31688 "2019-12-14T23:23:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ebg/32/23208_2.png) [@ebg](https://forums.swift.org/u/ebg)\
**Post date:** [December 14, 2019, 11:23pm UTC](https://forums.swift.org/t/dynamicmemberlookup-keypaths-not-working-with-generic-classes/31688/1 "2019-12-14T23:23:17Z")

</div>

I've added dynamicMemberLookup keypath support to a generic class in hopes of achieving forwarding to the underlying type. This compiles and runs, and seems like it will work:

```
@dynamicMemberLookup
open class BaseClass<GenericType> {
    public var child: GenericType!

    subscript<T>(dynamicMember keyPath: KeyPath<GenericType, T>) -> T {
        return child[keyPath: keyPath]
    }

    // ... impl omitted
}

public protocol Specialization: AnyObject {
    var prop1: String { get }
    var prop2: String { get }
}

public final class SubClass: BaseClass<Specialization> {
    // ... impl omitted
}

```

While the above compiles, I am unable to use dynamicMemberLookup to proxy through to `var child` as I'd expect. This doesn't compile:

```
let sub: SubClass<Specialization> = // ... impl omitted
print(sub.child.prop1) // ok
print(sub.prop1) // ERROR: Use of unresolved identifier

```

This should work, right?

---

<div class="post-metadata">

**Author:** ![broadway\_lamb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/broadway_lamb/32/665_2.png) [@broadway\_lamb](https://forums.swift.org/u/broadway_lamb)\
**Post date:** [December 14, 2019, 11:46pm UTC](https://forums.swift.org/t/dynamicmemberlookup-keypaths-not-working-with-generic-classes/31688/2 "2019-12-14T23:46:58Z")

</div>

Which version of Swift are you using?

I have a different error:

```swift
let sub: SubClass<Specialization> = SubClass() // error: Cannot specialize non-generic type 'SubClass'
print(sub.child.prop1) // no error
print(sub.prop1) // no error

```

Replacing `let sub: SubClass<Specialization> = SubClass()` with `let sub: SubClass = SubClass()` makes it compile alright.

---

<div class="post-metadata">

**Author:** ![ebg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ebg/32/23208_2.png) [@ebg](https://forums.swift.org/u/ebg)\
**Post date:** [December 15, 2019, 4:22pm UTC](https://forums.swift.org/t/dynamicmemberlookup-keypaths-not-working-with-generic-classes/31688/3 "2019-12-15T16:22:35Z")

</div>

It turns out Xcode just wasn't give me the most pertinent error which would've identified the issue immediately. It was saying

`Use of unresolved identifier`

instead of saying

`'subscript' is inaccessible due to 'internal' protection level`

Thanks!
