Skip to content

Conversation

@tayloraswift
Copy link
Member

Efficient Dictionary.mapValues with key context

I propose adding an overload to Dictionary.mapValues (and OrderedDictionary.mapValues) that passes the Key to the transformation closure.

This enables us to transform dictionary values with their associated key context without incurring the performance cost of rehashing (or in the case of reduce, reallocating) the dictionary storage, which is currently unavoidable when using init(uniqueKeysWithValues:) or reduce(into:).

@tayloraswift tayloraswift marked this pull request as ready for review January 2, 2026 21:15
@rjmccall rjmccall added LSG Contains topics under the domain of the Language Steering Group new proposal Adds a new proposal document labels Jan 5, 2026

### Alternative naming

The original draft of this proposal planned on overloading the existing `mapValues` method to accept a closure that takes both `Key` and `Value`. This was discovered to be source-breaking in rare scenarios where `mapValues` was being called on a dictionary with a 2-tuple value type. Thus, the new name `mapValuesWithKeys` was chosen to avoid source compatibility issues.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be interesting to think about how we'd handle incremental migration to a world in which mapValues provides the key to the transform (which I think most of us is the more desirable design). It ought to be possible to do so by doing something like introducing dedicated naming for both operations, and then using a feature flag to toggle which one is bound by the mapValues name, and we could then set that feature flag in a future language version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a section at the end


### Adoption of typed `throws` for pre-existing methods

The proposed `mapValuesWithKeys` method uses typed `throws`, unlike the existing `mapValues` method which uses untyped `throws`. In the future, we may wish to introduce a typed `throws` variant of `mapValues` as well, and take the API break opportunity to select a new name for this method, which would then enable the standard library to eventually reassign the `mapValues` name to the version that supplies key context to the transformation closure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typed-throw adoption is not an API break (also, the PR to adopt typed throws for mapValues should land soon). So I think typed-throws adoption is unrelated.

Rather, we would do something like give the existing mapValues an explicit name like mapValuesWithoutKeys and then tie the meaning of mapValues to an upcoming language feature (upcoming library feature?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, i rewrote that paragraph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LSG Contains topics under the domain of the Language Steering Group new proposal Adds a new proposal document

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants