-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Editorial: Define SortCompare as an Abstract Closure #2606
Editorial: Define SortCompare as an Abstract Closure #2606
Conversation
spec.html
Outdated
<dt>description</dt> | ||
<dd>It performs a numeric comparison rather than the string comparison used in <emu-xref href="#sec-array.prototype.sort"></emu-xref>.</dd> | ||
</dl> | ||
<emu-alg> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the next lines are just indented.
_comparefn_
to SortCompare_comparefn_
to SortCompare
3e14d34
to
9c212d8
Compare
This helps progress on #1884. |
spec.html
Outdated
_x_: a Number or BigInt, | ||
_y_: a Number or BigInt, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_x_: a Number or BigInt, | |
_y_: a Number or BigInt, | |
_x_: a Number or a BigInt, | |
_y_: a Number or a BigInt, |
9c212d8
to
1698647
Compare
This doesn't quite work as written because the spec talks about whether SortCompare is a consistent comparison function, which is a function of two parameters rather than 3. The approach @jmdyck takes to resolving this in #2305 (which is blocked on, that PR has unintended normative consequences) is to make an abstract closure which captures the relevant parameters, so that we can talk about whether that abstract closure is a consistent comparison function without needing to change that definition. That seems like a good approach to take here while we work on #2305. |
fee77ec
to
05dc1a2
Compare
_comparefn_
to SortCompare@@ -37544,7 +37544,22 @@ <h1>Array.prototype.sort ( _comparefn_ )</h1> | |||
1. Append _kValue_ to _items_. | |||
1. Set _k_ to _k_ + 1. | |||
1. Let _itemCount_ be the number of elements in _items_. | |||
1. [id="step-array-sort"] Sort _items_ using an implementation-defined sequence of calls to SortCompare. If any such call returns an abrupt completion, stop before performing any further calls to SortCompare or steps in this algorithm and return that completion. | |||
1. [id="step-array-sort-sortcompare"] <a name="sec-sortcompare"></a> Let _sortCompare_ be a new Abstract Closure with parameters (_x_, _y_) that captures _comparefn_ and performs the following steps when called: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<a name="sec-sortcompare"></a>
is so that existing links to the spec with #sec-sortcompare
continue working:
- Is that a goal we have?
- Is there a better way to do it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've seen other sections with an oldids
attribute, though not sure if that would work in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that a goal we have?
Yes, but only weakly - we try to make old links keep working when we can point to roughly the place, but sometimes we can't and that's fine.
Is there a better way to do it?
oldids
on the containing clause is probably the way to go. It won't point to exactly this step, but it's close enough.
Do we think we could split the difference and still retain the bulk of the steps as a seperate AO, reducing the steps in the abstract closure? That would make it easier to share with Alternatively that could be done as part of the change-array-by-copy proposal itself. |
I'm going to close this in favor of #2305, now that that PR has been updated to be strictly editorial. They're basically the same, except the older #2305 also refactors the definition of @acutmore: Yes, you could define a new AO (say |
This PR is extracted from a PR to the "Change Array by Copy" proposal (tc39/proposal-change-array-by-copy#69). Thanks @ljharb for pointing out that we can bring this improvement independently from that proposal.
The
SortCompare
AO implicitly receives_comparefn_
fromArray.prototype.sort
, in a way that looks like a closure (but I don't think that it is, sinceSortCompare
is an AO and not an Abstract Closure defined in theArray.prototype.sort
steps). This PR passes_comparefn_
explicitly as a parameter.I also did the same thing for
TypedArraySortCompare
, that implicitly receives both_comparefn_
and_buffer_
.