Dynamic key path member lookup cannot refer to static member

@dynamicMemberLookup
struct StaticLens<T> {
    subscript<U>(dynamicMember keyPath: KeyPath<T.Type, U>) -> U {
        // Note that we are explicitly referring to static member, not instance member.
        return T.self[keyPath: keyPath]
    }
}

struct S {
    static let a = 1
}

// error: Dynamic key path member lookup cannot refer to static member 'a'
StaticLens<S>().a

Is this intended?

1 Like

Hello,

No, it is not. All we need is a little energy:

[...] if there was a proposal and working implementation, it would very likely be accepted.

Thanks, I thought it was fixed. Sorry for bring it back.

Edit: I think the error message is misleading, is it worth filing a bug?

I would phrase it a little differently from Gwendal: Static key paths are not currently supported by Swift. However, this is basically just a gap in the language—it’s pretty clear how they would work and they would slot right in to the existing features. Someone does need to implement and propose them, though.

4 Likes
Terms of Service

Privacy Policy

Cookie Policy