[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift
me at benrimmington.com
Thu Apr 6 08:55:13 CDT 2017
> On 6 Apr 2017, at 04:13, Xiaodi Wu wrote:
>> On Wed, Apr 5, 2017 at 9:21 PM, Ricardo Parada wrote:
>>> On Apr 5, 2017, at 9:41 PM, Brent Royal-Gordon wrote:
>>> It's worth noting that, if you write `\Person.name.valueType`, this syntax is ambiguous—it could mean "make a key path for the `valueType` property on `name` property of `Person`", or it could mean "make a key path for the `name` property of `Person`, then access the key path's `valueType` property". We can solve this by always interpreting it as the former and requiring parentheses for the latter—that is, `(\Person.name).valueType`—but I thought it was worth calling out explicitly.
>> Good point.
>> I'm thinking about the hypothetical code examples from previous emails:
>> let isPuppyQualifier = \Pet.type == .dog && \Pet.age < 12
>> let familyQualifier = (\Family.pets).contains(where: isPuppyQualifier)
>> let familiesWithPuppies = Family.fetch(editingContext, familyQualifier)
> That's an interesting point. While `\` alone seems acceptable, I think it's unfortunate that we'll have `(\...)` and `\(...)` both in the language.
> Can we maybe consider instead:
> let firstFriendsNameKeyPath = \Person.friends.name\
'Single quotes' (i.e. U+0027 APOSTROPHE) are available AFAIK:
// Create a key path and use it
let firstFriendsNameKeyPath = 'Person.friends.name'
luke[keyPath: firstFriendsNameKeyPath] // "Han Solo"
// or equivalently, with type inferred from context
luke[keyPath: '.friends.name'] // "Han Solo"
// [SE-0042][SR-3550] Unapplied method references
'String.lowercased()' // (String) -> String
'String.lowercased(with:)' // (String, Locale?) -> String
Unlike the Lisp-style backtick, an apostrophe would appear on *both* ends of the key path (or method reference).
More information about the swift-evolution