[swift-evolution] Smart KeyPaths

David Smith david_smith at apple.com
Fri Mar 17 16:09:05 CDT 2017

> On Mar 17, 2017, at 1:58 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>> On Mar 17, 2017, at 10:04 AM, Michael LeHew via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Introduction
>> We propose a family of concrete Key Path types that represent uninvoked references to properties that can be composed to form paths through many values and directly get/set their underlying values.
> I don't know how to express the level of "Yes" I feel in response to this proposal without using language that's inappropriate on a public mailing list.
> A few questions:
> How do key paths interact with Optionals? Can you form a key path to `Person.bestFriend.name`, and is that the syntax, or is it `Person.bestFriend?.name`?

Yes and yes. You'll just end up with Value being an optional type.

> Foundation key paths have a sometimes-useful property where, if they traverse a collection, the result becomes a collection of the ending type. Is a similar feature planned for smart key paths—perhaps something like `Person.friends[].name`?

We've talked about stuff like this, but a number of the obvious approaches to it end up impacting our ability to make key paths Codable in the future. We can get many of the KVC collection operators with conditional extensions, e.g.

extension Collection where Element : Numeric /*or whatever it ended up being named, I'm behind on email */ {
    static let sum : Int

And then you could do Person.friends.sum where in ObjC you would have done @"friends. at sum", and so on.

> Given that Swift has a syntax called #keyPath which is unrelated to these "key paths", have you considered using a different name to avoid confusion? Maybe "property paths" or "accessor paths"?

We're hoping we can make that syntax basically vestigial by providing overlays that take the new types. It never worked outside of Darwin platforms anyway.


> -- 
> Brent Royal-Gordon
> Architechies
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170317/69b89f66/attachment.html>

More information about the swift-evolution mailing list