News for hackers. Stuff that matters.
Re:TypeScript 5.2's New Keyword: 'using' by masklinn on Monday June 19, 2023 @11:41AM (#36392360)
You're telling it through the `using` statement?
Re:TypeScript 5.2's New Keyword: 'using' by tantalor on Monday June 19, 2023 @04:45PM (#36396233)
I mean the implementation of the polyfill, in JS
Re:TypeScript 5.2's New Keyword: 'using' by Dylan16807 on Tuesday June 20, 2023 @02:50PM (#36408720)
That has the same answer. I'm not sure what you're looking for.

The polyfill adds code at the start of the block to make an array, and code at the end of the block to call dispose on everything in the array. Then the transpiler just has to turn "using" into an append.

There's some other details to handle exceptions but that's the basics of how it works.

It doesn't know what "can" be disposed, and neither will the native implementation. It does `item?.[Symbol.dispose]()` on each item you give it, and if that fails then you get an error.

Re:TypeScript 5.2's New Keyword: 'using' by tantalor on Monday June 26, 2023 @10:34AM (#36480144)
Thanks, that's exactly what I was wondering.

What happens if you save a reference to (or return, etc.) the variable you declared with "using"? Then it will end up automatically disposed, but from the programmers point of view it should still be usable (not disposed).

Re:TypeScript 5.2's New Keyword: 'using' by Dylan16807 on Monday June 26, 2023 @02:08PM (#36483420)
> from the programmers point of view it should still be usable (not disposed)

Then that programmer had their hopes too high. The basic "using" keyword is not for variables you want to move between scopes. Javascript doesn't do reference counting.

For saving/returning, you need to use explicit instances of DisposableStack and .move()