KeyPaths and PartialKeyPaths - when can you leave out the class?

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

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Filed as https://bugs.swift.org/browse/SR-6740

-Kenny

···

On Jan 11, 2018, at 3:12 PM, Jordan Rose <jordan_rose@apple.com> wrote:

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 <http://bugs.swift.org/>.

Jordan

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

+1

An example I ran in to today:

func dumpKeys<T>(of object: T, _ keypaths: PartialKeyPath<T>...) {
  for kp in keypaths {
    print("-", "\(kp): \(object[keyPath: kp])")
  }
}

Then in LLDB, I want to be able to write:

dumpKeys(of: context, \.invalidateEverything, \.invalidateDataSourceCounts)

But the PartialKeyPath expressions are considered ambiguous :frowning:

- Karl

···

On 12. Jan 2018, at 02:20, Kenny Leung via swift-users <swift-users@swift.org> wrote:

Filed as https://bugs.swift.org/browse/SR-6740

-Kenny

On Jan 11, 2018, at 3:12 PM, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

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 <http://bugs.swift.org/>.

Jordan

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

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users