Pitch: Support for map and flatMap with smart key paths

I recognize that this thread hasn't been touched in over a year, but I was wondering if this ever actually ended up going anywhere?

It received very positive feedback and I recently needed this functionality and guess I sort of expected it to be included in swift 4.2, because I was surprised that I couldn't map over a KeyPath.

Would the next step be to make this into an official proposal?

3 Likes

I think what’s keeping this from getting implemented is that key paths should really behave more like functions (ie. KeyPath<T, U> should be a subtype of T -> U or something like that), and then we would get mapping for free. So the last time this came up, the conclusion was that adding a separate overload of map and flatMap for keypaths would be pure sugar and we should come up with a more general solution.

Ah, found it, quoting @Joe_Groff here:

Instead of introducing a subtype relationship, we could (and should) extend the key path literal syntax to be valid in function type context, which I think covers the most common use case for this conversion of passing a literal property reference as an argument to map and friends.

6 Likes

It seems the thread you've linked to is the real pitch we should be discussing since its purpose is for more than just map/flatMap