Remix.run Logo
codedokode 2 days ago

Is "defer" a good pattern? Aren't constructors/destructor pairs, like in C++/Rust, or "with" blocks in Python, better, because you cannot forget to call the destructor. I the first code example in the article, it is easy to omit "defer" and the compiler won't notice.

shortercode a day ago | parent | next [-]

Defer is easier to fit into TS/JS as GC isn’t observable ( it kinda is with FinalisationRegistry but with a huge list of gotchas ).

Defer only cares if the variable leaves scope. At the end they mention the new “using” syntax which only requires you to mark the declaration, not requiring an additional statement for the cleanup. It’s kinda nice but the resource needs to implement a cleanup method making it less flexible than defer.

a day ago | parent | prev [-]
[deleted]