tc39 / proposal-explicit-resource-management

ECMAScript Explicit Resource Management
https://arai-a.github.io/ecma262-compare/?pr=3000
BSD 3-Clause "New" or "Revised" License
725 stars 29 forks source link

What does happen when using `let x = disposable()`? #182

Closed ClementNerma closed 1 year ago

ClementNerma commented 1 year ago

This may have been answered already, but I can't find anything on this, so here I go:

Let's say we have a function returning an object that implements the [Symbol.dispose]() method. The normal usage is using x = disposable();

At the end of the block, the dispose method is called.

But what happens if we write let x = disposable(); ? Or const x = disposable(); ? Is this a runtime error / type error, does this run normally without running the dispose method at the end of the scope?

This seems like a basic question so I'm pretty sure it's been answered somewhere already but I didn't manage to find anything on this question, so I'd appreciate if someone can explain this to me :)

ljharb commented 1 year ago

It works fine, but doesn't run the dispose method.

ClementNerma commented 1 year ago

It works fine, but doesn't run the dispose method.

Isn't this a footgun? There will 100% be cases where someone forgets to use using and uses let or const instead, and gets bugs because the resource hasn't been disposed of (like a database connection or a file handle not closing itself), which could be hard to debug especially in async. scenarios.

ljharb commented 1 year ago

The proposal is called "explicit resource management", so like many other parts of programming, yes, if you forget to take the right steps, the right thing won't happen.

This seems like it may be a duplicate of #159.

ClementNerma commented 1 year ago

I think it can be considered a duplicate yes, so I'm closing this in favor of #159