[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift
sean at fifthace.com
Thu Apr 6 10:08:55 CDT 2017
>> 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).
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.
More information about the swift-evolution