[swift-evolution] [Accepted] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift

Douglas Gregor dgregor at apple.com
Mon Apr 17 18:21:41 CDT 2017


Proposal Link: https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md <https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md>

The second review of SE-0161 "Smart KeyPaths: Better Key-Value Coding for Swift” ran April 5...9, 2017. The first review <https://lists.swift.org/pipermail/swift-evolution-announce/2017-April/000342.html> established the main substance of the proposal, while the second review focused mostly on the more lightweight “\” syntax. The proposal is accepted with one clarification regarding the intersection between contextual keypaths (where the base type is left unspecified) and array types.

The core team felt that the leading “\” is our best option for introducing keypaths. It is lightweight, yet provides a visual “escape” to indicate that the evaluation of the entities being referenced is being delayed. As noted in the result of the first review <https://lists.swift.org/pipermail/swift-evolution-announce/2017-April/000342.html>, this is part of a longer-term plan to use the “\” for unapplied method references, bringing the two closely-related features into syntactic alignment over time and providing an opportunity to stage in the important but currently-source-breaking changes accepted in SE-0042 <https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md>.

Much discussion centered around potential ambiguities. Accessing a property of a key path, e.g., \Person.friends[0].firstName.someKeyPathProperty, requires one to parenthesize the key path as (\Person.friends[0].firstName).someKeyPathProperty,  which is a common way to disambiguate expressions in Swift (and most other programming languages) already. More concerning to the core team was the ambiguity between contextual keypaths starting with a subscript and keypaths rooted on an array type, e.g.,

	\[a]?.foo     // is [a]? a type rooting a keypath “.foo”
                      // or is it a contextual keypath starting with a subscript [a]?

To resolve this ambiguity, *all* contextual keypaths will start with “\.”. The expression above will use the first interpretation (a type [a]? rooting a key path “.foo”), while the second interpretation would be written as:

	\.[a]?.foo    // a contextual key path starting with the subscript [a]

The core team felt that the result is more consistent: “\.” indicates a contextual keypath, while “\” without the “.” indicates a keypath with a specified root.

The authors will revise the proposal according. Thank you to everyone who participated in the reviews of this much-anticipated feature!

	- Doug
	Review Manager

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170417/63e42700/attachment.html>


More information about the swift-evolution mailing list