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

What happens if the object escapes? #161

Open
Lexicality opened this issue Jun 19, 2023 · 3 comments
Open

What happens if the object escapes? #161

Lexicality opened this issue Jun 19, 2023 · 3 comments
Labels
documentation Indicates a change to the documentation/explainer for the propsal.

Comments

@Lexicality
Copy link

I'm assuming this should have an obvious answer but I can't see one in the README.

If I have

function foo() {
    using reader = stream.getReader();
    return () => reader.read();
}

what happens if I call foo()()?

@NWYLZW
Copy link

NWYLZW commented Jun 19, 2023

I think it should not be allowed to hijack variables declared by using through closures.

@rbuckton
Copy link
Collaborator

@Lexicality:

what happens if I call foo()()?

It depends on the behavior of reader.read(). In this example, reader would be closed as soon as foo() returns. Assuming read() requires an open stream, I would expect reader.read() would throw an exception when you call the resulting arrow function.


@NWYLZW:

I think it should not be allowed to hijack variables declared by using through closures.

This was discussed in #97 and in plenary. To avoid this would require re-entering TDZ, and implementers were concerned about the impact a new TDZ would cause. In addition, a well-written disposable resource needs to defend against using that resource when it is in an unusable state, since those resources could be created without using when composing multiple resources.

There are also valid use cases for being able to access the resource after it has been disposed, such when the resource was used as a key in a Map. Even after the resource is disposed you may want to remove the key.

@Lexicality
Copy link
Author

@rbuckton ok so the answer is that the object gets disposed of no matter what.

I think it would be handy to call this out explicitly in the readme? It makes sense that closure lifetimes are ignored but it's not always obvious with these things

@rbuckton rbuckton added the documentation Indicates a change to the documentation/explainer for the propsal. label Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Indicates a change to the documentation/explainer for the propsal.
Projects
None yet
Development

No branches or pull requests

3 participants