-
Notifications
You must be signed in to change notification settings - Fork 32
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
{}
block can change a program's control flow when using
is used
#197
Comments
Yes, that's the point of it. We were aware of this when designing the feature and consider it to be acceptable. |
I don't know how to justify this control flow implication on What should I tell my colleagues who don't know about |
Any I believe this is the trade-off that has to be made for allowing inline RAII style. The alternative requires declaring all resources when opening the block, which has poorer ergonomics, and still implies code execution when exiting the block, but is maybe more "balanced" and thus more noticeable. |
You should send them a link to the MDN page on |
FWIW, It would have been great if we could have marked the block exit more clearly as an execution / interleaving point. Maybe someone can come up with a code editor tool that automatically adds a comment to these block exits. |
I really don't see how const stack = new DisposableStack();
try {
const x = stack.use(X());
const y = stack.use(Y());
console.log("done");
} finally {
stack.dispose();
} It solves all issues in the Motivations, with only the cost of creating a stack and adding a single const stack = new DisposableStack();
try {
const x = stack.use(X());
{
const y = stack.use(Y());
}
console.log("done");
} finally {
stack.dispose();
} |
In my original suggestion against adding syntax, I suggested abusing the for (const { using } of Disposable) {
const resource = using(getResource());
resource.doSomething();
const other = using(resource.getOther());
const stuff = other.doStuff();
using(() => cleanUpStuff(stuff));
} // automatically cleanup, even when something throws You can still write an adapter today on top of |
When this proposal was first introduced, the syntax was similar to C#'s earliest version of using (const x = y) {
} Over the many years that this proposal has been discussed, there was more and more interest within the committee to switch to an RAII-style form, and the consensus was to switch entirely to the RAII form. Even C# has adopted the RAII form for |
Though I can't say if I like the idea of abusing Any solution that creates a seperate scope for resource binding can avoid this issue.
I don't know having the committee knowing the implication is a good justification to impose this behavior for all JavaScript developers. Many JavaScript developers don't have the experience with RAII like C# or C++, and having this new syntax breaking a well established instinct on how I think we should raise this behavior change on |
Observe the following two programs:
Both programs would throw the same error, but the first program would print the log while the second won't.
This behavior may be surprising to many people as we are used to the fact that a
{}
block should not affect the control flow, but withusing
,{}
can change your program's control flow.The text was updated successfully, but these errors were encountered: