[swift-users] KeyPaths and PartialKeyPaths - when can you leave out the class?

Kenny Leung kenny_leung at pobox.com
Thu Jan 11 16:41:18 CST 2018


Hi All.

I’d like to be lazy and leave out the classname on a key path whenever possible. I thought that if PartialKeyPath was specified as the argument type, that meant the compiler should be able to figure out what the class is, but this does not seem to be the case.

Here’s an example that works.

class ThePath {
    var isWinding:Bool?
}

func walk<T>(aPath:T, forKey:PartialKeyPath<T>) {
}

func walkThePath(aPath:ThePath, forKey:PartialKeyPath<ThePath>) {
}

func test() {
    let path = ThePath()
    walkThePath(aPath:path, forKey:\ThePath.isWinding)
    walk(aPath:path, forKey:\ThePath.isWinding)
}

If you do this then it complains:

func test() {
    let path = ThePath()
    walkThePath(aPath:path, forKey:\.isWinding) // Expression type '()' is ambiguous without more context
    walk(aPath:path, forKey:\.isWinding)        // Type of expression is ambiguous without more context
}

Should I be able to do this?

Thanks!

-Kenny

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20180111/502a699d/attachment.html>


More information about the swift-users mailing list