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

Karl Wagner razielim at gmail.com
Sun Jul 16 14:55:03 CDT 2017


> On 12. Jul 2017, at 22:21, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> on Tue Jul 11 2017, Robert Bennett <rltbennett-AT-icloud.com> wrote:
> 
>> Just realized that even inout functions don’t let you do
>> member(object) = value. 
> 
> The other difference is that an inout function can't be used to get a
> member from an immutable value, whereas a keypath/subscript/property
> access can.
> 
> 
> -- 
> -Dave
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

Hmm… I forgot about setters.

I would still like some simple way to use a KeyPath where an equivalent closure would be expected (for getters AND setters). The operator approach looks like the best one, but I’d prefer a prefix operator, so…

\MyObj.something.name  // returns KeyPath<MyObj, String>

^\MyObj.something.name // returns (MyObj)->String
^myKeyPath             // as above.

*\MyObj.something.name // returns (inout MyObj, String)->Void
*myKeyPath             // as above.


Where those operators could, either now or later, be backed by some kind of protocol (similar to Equatable/Comparable) to express general-purpose callable objects.

But I don’t like how cryptic it all looks.

- Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170716/5a322211/attachment.html>


More information about the swift-evolution mailing list