proposal-explicit-resource-management icon indicating copy to clipboard operation
proposal-explicit-resource-management copied to clipboard

What happens if the object escapes?

Open Lexicality opened this issue 1 year ago • 3 comments

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()()?

Lexicality avatar Jun 19 '23 09:06 Lexicality

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

NWYLZW avatar Jun 19 '23 10:06 NWYLZW

@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.

rbuckton avatar Jun 19 '23 14:06 rbuckton

@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

Lexicality avatar Jun 19 '23 17:06 Lexicality