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.