Here's some hypothetical code example. Let suppose an extension on key paths allows us to invoke desc and asc to help create sort orderings. This is what it would look using the two different notations:
// Order by totalAmount descending, then by address.zip ascending let orderings = [ (\Order.totalAmount).desc, (\Order.address.zip).asc ] let orderings = [ \Order.totalAmount\.desc, \Order.address.zip\.asc ] The second one saves one character every time you want to invoke a method on the key path but I would have to go back and add the left parenthesis, which could be a bit annoying as I'm typing these things in, unless the editor were to come to the rescue add the left parenthesis automatically somehow. But anyways, I can go with either one. Still +1 on the proposal. :-) > On Apr 5, 2017, at 11:13 PM, Xiaodi Wu <[email protected]> wrote: > > On Wed, Apr 5, 2017 at 9:21 PM, Ricardo Parada via swift-evolution > <[email protected] <mailto:[email protected]>> wrote: > >> On Apr 5, 2017, at 9:41 PM, Brent Royal-Gordon via swift-evolution >> <[email protected] <mailto:[email protected]>> 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\ > > It is also worth mentioning that, with the sigil, the `keyPath` label may not > be so necessary: > > print(luke[\.friends[0].name]) > // or, if the suggestion above is accepted > print(luke[\.friends[0].name\])
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
