tc39 / proposal-set-methods

Proposal for new Set methods in JS
https://tc39.github.io/proposal-set-methods/
Other
655 stars 15 forks source link

Why normalize zero values? #97

Closed Josh-Cena closed 1 year ago

Josh-Cena commented 1 year ago

Every method that produces a set does a "If nextValue is -0š¯”½, set nextValue to +0š¯”½.". This looks unnecessary because we don't do the same normalization on this, and neither is this normalization expected. Since we use SameValueZero, there's no need to normalize zeros. This seems to be planned for removal: https://github.com/tc39/proposal-set-methods/pull/75#discussion_r974662029 but never addressed.

bakkot commented 1 year ago

I'm not sure I understand the question. Sets cannot contain -0 by construction. So whenever we're constructing a set by creating its [[SetData]] slot directly, we need to avoid putting -0 in it.

Since we use SameValueZero, there's no need to normalize zeros.

I don't know which use of SameValueZero you're talking about. There's one in SetDataHas, but that's not always sufficient - for example, consider

let minusZeroSet = new Set([0]);
minusZeroSet.keys = () => [-0].values();
console.log([...(new Set).union(minusZeroSet)]);

Without the normalization in step 7.b.ii of Set.prototype.union, that would result in [-0], which would be bad.

This seems to be planned for removal: https://github.com/tc39/proposal-set-methods/pull/75#discussion_r974662029 but never addressed.

The comment was addressed; the unnecessary cases were removed.

Josh-Cena commented 1 year ago

Oopsā€”you are right, sets cannot contain -0. That was my main confusion.