@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
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.