This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
using await
/await using
/async using
#12
Comments
cc: @syg |
Note that |
@rbuckton We discussed syntax options on last week JSCIG meeting, here is the poll result: https://github.com/JSCIG/es-discuss/blob/master/feedback/202303-async-using.md . We had 8 respondents, only 1 choose |
Per the March plenary, the consensus was to use Fixed by #15 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
In the January, 2023 TC39 plenary session there was a debate on what the final syntax for the asynchronous
using
declaration should be, given that there are conflicting developer intuitions regarding each potential syntax form.The three forms currently under consideration are:
using await x = y
(currently proposed)async using x = y
await using x = y
We opted to postpone advancement until the March, 2023 plenary to give some delegates the opportunity to conduct informal surveys to determine which of these options more clearly represented the semantics of the proposal. If there is no clear choice, the proposal will advance to Stage 3 at the March, 2023 plenary using the proposed syntax.
There are pros and cons to each syntax option, which I will summarize below.
using await x = y
The current proposal syntax was chosen prior to the January, 2023 plenary for a number of reasons. The use of the
await
keyword very clearly indicates an asynchronous interleaving point, much like AwaitExpression and thefor-await-of
statement. In addition, theusing await
keyword order was chosen to avoid ambiguity with anawait using
expression, given thatusing
on its own remains a valid identifier, and thatawait
as a modifier currently only occurs to the right of the statement it modifies (i.e.,for await
).However, there is the potential for confusion for code readers due to the non-local nature of the
using
declaration. The actualawait
occurs as control flow exits the block scope, rather than immediately at the site of the declaration. This may lead some to incorrectly assume thatusing await x = y
would await the value ofx
/y
. In the case of afor await (const x of y)
statement, thefor await
operation doesn'tawait
the value ofy
, though it doesawait
the result of eachnext()
operation on its iterator, which in turn means that it doesawait
the value that becomesx
. This potential conflation of meanings can lead to confusion if attempting to intuit the meaning ofusing await
givenfor await
as context.async using x = y
The
async using
syntax form was initially suggested as a means to pair with a specially annotatedawait using { ... }
block, so as to resolve #1. However, we were able to resolve #1 without the introduction of a new block syntax, which is why we did not choose prior to the January, 2023 plenary.The advantage of the
async using
syntax is that it indicates an asynchronous operation without conveying a meaning that the operation might occur immediately. However,async using
breaks with existing uses ofasync
in the language today. Currently,async
is only used to indicate functions that have different runtime semantics than a normal function, permitting the use ofawait
expressions and thefor-await-of
statement in the function body. Yet this use doesn't indicate an interleaving point will occur, which breaks from the intended semantics of this proposal. This meaning ofasync
is further reinforced by proposals likeasync do {}
, which would still require an explicitawait
somewhere to observe the result.Also, while a minor concern,
async using
will likely need a cover grammar to disambiguate between anasync using
declaration and anasync using =>
declaration.await using x = y
A third option we've discussed outside of plenary would be to use an
await using
ordering instead. This matches the C# syntax that is the equivalent of this behavior, and has the benefit of continuing to useawait
to indicate an async interleaving point. This also has a slight advantage overusing await
, given that the keyword order may lean more towards an interpretation of "await
theusing
ofx
". It is unclear, however, if this distinction is enough to guide developer intuition.As with
async using
, there is a minor concern regarding the need for a potential cover grammar to disambiguateawait using
as a declaration fromawait using
(orawait using.x
) as an expression.Not considered:
using async x = y
We are not currently considering a
using async
keyword order at this time. We feel it is important to align the sync and async versions of the proposal, and are concerned that the ambiguity between:would lead to further confusion. We could ban
async
as an identifier inusing
, as we've currently done forawait
(and for the same reason), however there is a distinction betweenawait
andasync
as identifiers. Currently,await
is a reserved word, making it a syntax error to use it as an identifier in strict-mode code. It is also a syntax error to use it as an identifier in an async function, leaving it as only legal in non-async, non-strict code.async
, on the other hand, is not reserved in any mode, nor is it reserved in async functions, so we are concerned that banningasync
for this purpose would get in the way of potential refactors in existing code, such as:Due to the existing restrictions the language imposes on the use of
await
as an Identifier, we're far less concerned aboutusing await
being a refactoring hazard.The text was updated successfully, but these errors were encountered: