[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift

Ricardo Parada rparada at mac.com
Thu Apr 6 10:29:06 CDT 2017


Another thought on the use of single quotes (an possibly the proposed escape character).  Do you think the closing single quote could be made optional since key paths cannot have spaces? Or would that be confusing for beginners?

For example, these two would be equivalent:

   let isPuppyPredicate = 'Pet.type == .dog && 'Pet.age < 12
   let isPuppyPredicate = 'Pet.type' == .dog && 'Pet.age' < 12

Or this:

   let isPuppyPredicate = \Pet.type == .dog && \Pet.age < 12
   let isPuppyPredicate = \Pet.type\ == .dog && \Pet.age\ < 12

You would use the closing character normally when wanting to invoke a method on the key path object.

Any thoughts?


> On Apr 6, 2017, at 11: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.  
> 
> 
>> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170406/f94829a6/attachment.html>


More information about the swift-evolution mailing list