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

Karl Wagner razielim at gmail.com
Tue Jan 16 10:53:05 CST 2018


+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 :(

- Karl

> On 12. Jan 2018, at 02:20, Kenny Leung via swift-users <swift-users at swift.org> wrote:
> 
> Filed as https://bugs.swift.org/browse/SR-6740 <https://bugs.swift.org/browse/SR-6740>
> 
> -Kenny
> 
> 
>> On Jan 11, 2018, at 3:12 PM, Jordan Rose <jordan_rose at apple.com <mailto:jordan_rose at 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 at swift.org <mailto: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 <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>> 
> 
> _______________________________________________
> 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/20180116/78c5d5b4/attachment.html>


More information about the swift-users mailing list