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

Jordan Rose jordan_rose at apple.com
Thu Jan 11 17:12:44 CST 2018


I'm not sure whether it was supposed to be supported or not, but either way it's a reasonable feature request. Please file at bugs.swift.org.

Jordan


> On Jan 11, 2018, at 14:41, Kenny Leung via swift-users <swift-users at swift.org> wrote:
> 
> 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
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

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


More information about the swift-users mailing list