[swift-evolution] [Pitch] KeyPath based map, flatMap, filter

Benjamin Herzog mail at benchr.de
Sat Jul 8 17:02:49 CDT 2017


The $ 'operator' would also be a good option because it matches the $0 syntax which is not far away logically.
______________________

Benjamin Herzog



> On 9. Jul 2017, at 00:00, Robert Bennett via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I’m not sure about needing a universal promotion operator in Swift. However, in the case of KeyPaths, I think a leading $ (not currently a valid operator) would work well.
> 
> prefix operator $
> prefix func $<T,U>(rhs: KeyPath<T,U>) -> (T)->U { return { $0[keyPath: rhs] } }
> 
> guys.map($\.name)
> 
> This reads really well to me because the $ is suggestive of the “functionization” of the KeyPath. Also, this is guaranteed to have no compatibility issues (right?) because it’s currently forbidden.
> 
> (I’m only suggesting giving the leading $ this functionality, not necessarily achieving this by making it a valid operator — in fact, it would probably be best if this functionality were “hard-coded” just as it is currently hard-coded for use in $0.)
> 
>> On Jul 8, 2017, at 5:46 PM, Benjamin Herzog via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Is this operator common in other languages? I would actually expect that the conversation is not 'almost-implicit' but completely implicit instead. I think both - a prefix and postfix operator - are not obvious enough what happens here, especially because this kind of conversion is not happening in other parts of the language.
>> All conversions are implicit (from explicit type to protocol, from Swift stdlib types to Objective-C types, from any type to Any, …) currently.
>> 
>> ______________________
>> 
>> Benjamin Herzog
>> 
>>> On 8. Jul 2017, at 22:10, Hooman Mehr via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> I like this promote operator idea. I have been defining similar operators for specific projects almost at random. It makes sense to come up with a well-defined behavior and name for such operators, as a common practice as you suggest.
>>> 
>>> The problem with the postfix operator is that it does not currently work without an extra set of parenthesis:
>>> 
>>> postfix operator ^
>>> 
>>> postfix func ^<T,U>(lhs: KeyPath<T,U>) -> (T)->U { return { $0[keyPath: lhs] } }
>>> 
>>> struct Guy { let name: String }
>>> 
>>> let guys = [
>>>     Guy(name: "Benjamin"),
>>>     Guy(name: "Dave"),
>>>     Guy(name: "Brent"),
>>>     Guy(name: "Max")
>>> ]
>>> 
>>> guys.map(\.name^) // Error: Invalid component of Swift key path
>>> 
>>> guys.map((\.name)^) // This works
>>> 
>>> Is this a bug?
>>> 
>>> That is the reason I used a prefix operator (~) in my suggestion in the a previous e-mail on this thread.
>> 
>> _______________________________________________
>> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170709/e4846e64/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: Message signed with OpenPGP
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170709/e4846e64/attachment.sig>


More information about the swift-evolution mailing list