Duplicate keys in map construction

We decided today that map:merge, map:of-pairs, and map:merge, if they encounter duplicate keys, should use the key associated with the first of the duplicates.

Unfortunately this cannot be implemented using map:put as a primitive, because `map:put($map, $key, $value)` is currently defined to return a map in which the new `$key` replaces the old. And that seems a reasonable expectation. It's also unambiguously how map:put() behaves in 3.1.

I considered using map:replace() instead of map:put(). The prose of map:replace suggests that the old key is retained, but the "formal equivalent" is defined using map:put(), which uses the new key.

So I'm going to suggest that the final map contains the LAST of the duplicate keys, at the position of the FIRST of the duplicates, simply because that's consistent with what a user-written implementation using map:put would do.

I sounds a bit weird, but the assumption is that it's an edge case that very few users will notice or care about.

Michael Kay
Saxonica

Received on Tuesday, 28 January 2025 23:00:36 UTC