Pitch: Key-Path Member Lookup

It looks like the compiler will be able to type check the key paths from T to U since Lens is generic over T. Will this usable for non-generic types? Could I write something like this? (I don't want to, just trying to get a sense of how this works.)

@keyPathMemberLookup
struct NotAnInt {
    var value: Int

    subscript<T>(keyPathMember path: WriteableKeyPath<Int, T> -> T {
        return value[keyPath: path]
    }
}

let x = NotAnInt(value: 4)
let y = x.nonzeroBitCount    // 1

This sounds pretty great. The big distinctions from @dynamicMemberLookup is that you have type checking the whole way through, and can return different types from the subscript, but I think reusing the attribute is still fine.

1 Like