[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift
Ricardo Parada
rparada at mac.com
Thu Apr 6 10:10:18 CDT 2017
I did not mention single quotes because I was assuming those were reserved for string literals.
But if they are available for something like this, it would make it obvious where the key path begins and where it ends. I went through my hypothetical code samples, and I don't mind the \ escape character. For instance:
let isPuppyPredicate = \Pet.type == .dog && \Pet.age < 12
let hasPuppiesPredicate = (\Family.pets).any(isPuppyPredicate)
let familiesWithPuppies = Family.fetch(editingContext, hasPuppiesPredicate)
I don't mind having to use parenthesis sometimes when invoking a method on key path for example.
With single quotes it would look like this:
let isPuppyPredicate = 'Pet.type' == .dog && 'Pet.age' < 12
let hasPuppiesPredicate = 'Family.pets'.any(isPuppyPredicate)
let familiesWithPuppies = Family.fetch(editingContext, hasPuppiesPredicate)
And creating orderings for example:
let orderings = [ 'Order.totalAmount'.desc, 'Order.address.zip'.asc ]
> On Apr 6, 2017, at 9:55 AM, Ben Rimmington via swift-evolution <swift-evolution at swift.org> wrote:
>
> Re: <https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md#referencing-key-paths>
>
>> 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[0].name\
>
> 'Single quotes' (i.e. U+0027 APOSTROPHE) are available AFAIK:
>
> // Create a key path and use it
> let firstFriendsNameKeyPath = 'Person.friends[0].name'
> luke[keyPath: firstFriendsNameKeyPath] // "Han Solo"
>
> // or equivalently, with type inferred from context
> luke[keyPath: '.friends[0].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).
>
> -- Ben
>
> _______________________________________________
> 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/20170406/23f34a62/attachment.html>
More information about the swift-evolution
mailing list