Skip to content

Commit

Permalink
Switch Map.these to yield key-value pairs instead of just values
Browse files Browse the repository at this point in the history
In chapel-lang#14718, it was proposed that iterating over a map should yield
key-value pairs as opposed to just values, so this PR implements
that change.

Signed-off-by: Ben McDonald <46734217+bmcdonald3@users.noreply.github.com>
  • Loading branch information
bmcdonald3 committed Feb 13, 2023
1 parent d831fa0 commit 2312ff6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions modules/standard/Map.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ module Map {
pragma "no doc"
type _lockType = ChapelLocks.chpl_LocalSpinlock;

/* A compile-time parameter to control the behavior of map iteration
When ``false``, the deprecated behavior is used (i.e., iterating over
the map will yield references to values in the map).
When ``true``, the new behavior is used (i.e., iterating over the
map will yield (key, value) tuples in the map).
*/
config param mapYieldTuple = false;

pragma "no doc"
class _LockWrapper {
var lock$ = new _lockType();
Expand Down Expand Up @@ -528,11 +536,21 @@ module Map {
}

/*
Iterates over the keys of this map. This is a shortcut for :iter:`keys`.
Iterates over the key-value pairs of this map. This is a shortcut for :iter:`items`.
:yields: A reference to one of the keys contained in this map.
:yields: A tuple whose elements are a copy of one of the key-value
pairs contained in this map.
*/
iter these() const ref {
iter these()
where mapYieldTuple {
for kv in this.items() {
yield kv;
}
}

deprecated "yielding only keys when iterating over 'Map' is deprecated, please set the config param :param:`mapYieldTuple` to 'true' to opt into yielding tuple (key, value) pairs instead"
iter these() const ref
where !mapYieldTuple {
for key in this.keys() {
yield key;
}
Expand Down

0 comments on commit 2312ff6

Please sign in to comment.