[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift
Vladimir.S
svabox at gmail.com
Thu Apr 6 10:31:50 CDT 2017
On 06.04.2017 18:10, Ricardo Parada via swift-evolution wrote:
> 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)
>
FWIW I find this much more clear and visually separated, my mind parses
this code much faster then previous code block.
Although in this case we have some ambiguity (at first look) if 'Pet.type'
is a string or escaped variable, but context will help very much when this
feature is used IMO.
In any case, all suggested variants have some kind of ambiguity, so
personally I think this is the best suggestion for now. Also, as was said,
mentally it is close to keypath/selector string in ObjC.
> 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 <mailto: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 <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
More information about the swift-evolution
mailing list