Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map/Set’s SameValueZero key/value mapping #383

Closed
bathos opened this issue Aug 30, 2023 · 2 comments
Closed

Map/Set’s SameValueZero key/value mapping #383

bathos opened this issue Aug 30, 2023 · 2 comments

Comments

@bathos
Copy link
Contributor

bathos commented Aug 30, 2023

%Map% and %Set% use SameValueZero semantics for key comparison, but they additionally normalize -0 to 0:

Map.prototype.set(key, value)

  1. If key is -0𝔽, set key to +0𝔽.

Set.prototype.add(value)

  1. If value is -0𝔽, set value to +0𝔽.

So for example, [ ...new Set([ -0 ]) ][0] is 0, not -0. More importantly this means new Set([ -0, 0 ]) and new Set([ 0, -0 ]) behave equivalently — you can’t observe which zero happened to be added first.

The proposal’s updated SameValueZero will have SameValueZero(#[ -0 ], #[ 0 ]) === true (which makes sense), but I would expect corresponding updates in %Map.prototype.set% and %Set.prototype.add% that canonicalize* the key to include, at depth, only positive zeros. Presumably this would be realized with a new shared abstract op (e.g. ToCanonicalSVZValue) that could also encapsulate the existing -00 behavior.

* This is spiritually related to #291, but observable non-canonical NaNs showing up in weird places is a meta-issue that isn’t specific to this proposal. The Map/Set negative zero canonicalization on the other hand is an existing concrete/262-specified behavior.

@acutmore
Copy link
Collaborator

Hi @bathos

This is discussed (at length) in #65

@bathos
Copy link
Contributor Author

bathos commented Aug 30, 2023

Thanks @acutmore, sorry for the dupe. At length indeed! I remember reading that a while ago now, but missed the conclusion that it was intentional to break from the existing Map/Set canonicalization behavior.

Though I personally believe that behavior should never have existed in the first place, it does and can’t go away now, so I think the decision to depart from it here is unfortunate — it (unintuitively) adds more quirky complexity to Map/Set’s keying semantics, it doesn’t reduce it — but I appreciate that this has already been discussed in depth.

@bathos bathos closed this as completed Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants