Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This looks most similar to golang’s defer. It runs cleanup code when leaving the current scope.

It differs from try/finally, c# “using,” and Java try-with-resources in that it doesn’t require the to-be-disposed object to be declared at the start of the scope (although doing so arguably makes code easier to understand).

It differs from some sort of destructor in that the dispose call is tied to scope, not object lifecycle. Objects may outlive the scope if there are other references, and so these are different.

If you like golang’s defer then you might like this.



> This looks most similar to golang’s defer. It runs cleanup code when leaving the current scope.

It's nothing like go's defer: Go's defer is function-scoped and registers a callback, using is block-scoped and registers an object with a well defined protocol.

> It differs from [...] c# “using,”

It's pretty much a direct copy of C#'s `using` declaration (as opposed to the using statement): https://learn.microsoft.com/en-us/dotnet/csharp/language-ref....

This can also be seen from the proposal itself (https://github.com/tc39/proposal-explicit-resource-managemen...) which cites C#'s using statement and declaration, Java's try-with-resource, and Python's context managers as prior art, but only mentions Go's defer as something you can emulate via DisposableStack and AsyncDisposableStack (types which are specifically inspired by Python's ExitStack),




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: