[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift
Douglas Gregor
dgregor at apple.com
Thu Apr 6 11:19:20 CDT 2017
> On Apr 6, 2017, at 8:13 AM, Ricardo Parada via swift-evolution <swift-evolution at swift.org> wrote:
>
> I agree, there's an analogy between strings and key paths, and in that regards the single quote would make sense. I would not complain.
The only analogy between strings and key-paths is that the existing Cocoa APIs for key-paths use strings. That’s not an analogy to hang language syntax on, because it’s relevance will fade quickly.
The core team discussed single quotes, and decided that we want to save them for something in the string/character realm.
- Doug
>
>
>> On Apr 6, 2017, at 11:08 AM, Sean Heber via swift-evolution <swift-evolution at swift.org> wrote:
>>
>>>> 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).
>>
>> For what it’s worth, I much rather prefer this approach and was going to suggest it today. In Objective-C, keypaths were just strings so by using a single tick, they still look *almost* like strings, but now they’re safe and checked by the compiler - magical.
>>
>> l8r
>> Sean
>>
>> _______________________________________________
>> swift-evolution mailing list
>> 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