[swift-evolution] SE-0062 Referencing Objective-C key-paths
kenny_leung at pobox.com
Thu Apr 14 13:37:28 CDT 2016
Aeons ago, I was on the team that invented key paths. There were some pretty major performance issues:
- the key path had to be separated into its individual components, requiring processing time and generating a bunch of garbage
- the obvious thing to do would have been to parse the key path into an array of strings up front, and then simply process the key path in a loop, but this was not possible because some objects might override valueForKeyPath and be expecting to parse their own portion of the key path (and down) in some special way
- pre-parsing may also be problematic because key paths could contain operators like @count, or @sum, @avg, etc.
That being said, it would certainly be nice if the compiler pre-parsed key paths into either:
- key path objects
- arrays of strings
- arrays of “key” objects
and KVC was modified to accept these as a lower level API.
Perhaps it would be prudent to pull in the Foundation guys on this and get their input before implementing something on the Swift side that would be hard to take back.
> On Apr 12, 2016, at 9:35 AM, Douglas Gregor via swift-evolution <swift-evolution at swift.org> wrote:
>> On Apr 7, 2016, at 9:34 PM, Les Pruszynski via swift-evolution <swift-evolution at swift.org> wrote:
>> This is my first post on this list so please bear with me.
>> I very much like this proposal but what bothers me is this doubling of valueForKeyPath(#keyPath(xxx) in the signature of the function.
>> chris.valueForKeyPath(#keyPath(Person.bestFriend.lastName)) // => Groff
>> .valueForKeyPath(#keyPath(Person.friends.firstName)) // => ["Joe", "Douglas"]
>> I’m not sure whether the form below is actually possible. For me it reads more naturally and is more consistent with “Modern Swift” as far as I know.
>> chris.valueFor(#keyPath(Person.friends.firstName)) // => ["Joe", "Douglas”]
>> or maybe
>> chris.valueOf(#keyPath(Person.friends.firstName)) // => ["Joe", "Douglas”]
> If key paths were some stronger type (as Brent is suggesting), we could do this. However, because key paths are just Strings, we need to compensate for weak type information, so valueForKeyPath should retain it’s currently name despite the redundancy in many use sites with #keyPath.
> - Doug
> swift-evolution mailing list
> swift-evolution at swift.org
More information about the swift-evolution