Adding Go's Defer to the TypeScript Compiler
Posted by healeycodes 17 hours ago
Comments
Comment by throwaway81523 16 hours ago
Comment by Rohansi 15 hours ago
Comment by kaashif 15 hours ago
Try X finally dispose of all resources. X, defer clean up X. Or how about just X and cleanup is done automatically, with the author of the resources deciding what is needed to clean X up.
Comment by mcintyre1994 15 hours ago
Comment by kaashif 14 hours ago
There isn't any way to forget to drop a resource if you have RAII.
Comment by throw-the-towel 12 hours ago
Comment by masklinn 9 hours ago
Comment by pjmlp 8 hours ago
For example, combining RAII with lock free data structures, which usually ends up in techniques like hazardous pointers instead.
Comment by okigan 12 hours ago
It's way easier to use, much clear, less typing, more predictable runtime behavior.
RAII... terrible name, great concept!
Comment by masklinn 9 hours ago
Traditionally langages with simpler runtimes simply used destructors for this, refcounting made it deterministic (modulo the old reference leak), but more advanced garbage collection schemes made that stop working.
Comment by pjmlp 8 hours ago
Comment by Rohansi 11 hours ago
RAII doesn't really fit into every language because they don't all have deterministic destructors/finalizers and objects with scoped lifetimes. Sometimes you only have one but not the other and you definitely need both.
Comment by okigan 6 hours ago
And regarding sibling post about finalizers -- another broken concept as you cant use finalizers with limited resources (ex. graphic contexts, db connections, handles, locks, etc) and then these languages that use finalizers become complete mess if you need to manage such resources.
Comment by wannabe44 9 hours ago
It's also more exception-safe when you have more than one throwing call in the try block.
Comment by healeycodes 8 hours ago
Comment by gtowey 11 hours ago
Comment by anttiharju 11 hours ago
One can just wrap os.Exit in a helper to get the expected behaviour.
Comment by Joker_vD 11 hours ago
Comment by cute_boi 11 hours ago
``` async function run() { try { await new Promise(() => { // The executor function finishes, // but it never calls resolve() or reject(). }); } finally { console.log("cleanup"); } } ```
I never expect cleanup to be logged.
Comment by codedokode 8 hours ago
Comment by shortercode 6 hours ago
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.
Comment by geostyx 12 hours ago
defer is the thing I miss the most coming from Go
Comment by healeycodes 8 hours ago
Comment by fg137 13 hours ago
Comment by healeycodes 8 hours ago
$ bat 2.ts
───────────────
1 │ function one() {
2 │ let x = 1;
3 │ defer (() => { throw new Error("thrown from one!"); })()
4 │ x = 2;
5 │ }
6 │ one();
─────┴─────────
$ node 2.js
~/healeycodes-typescript-go/2.js:29
throw _errors_1[0];
^
Error: thrown from one!
at _callee_1 (/Users/andrew/Documents/GitHub/healeycodes-typescript-go/2.js:9:46)
at /Users/andrew/Documents/GitHub/healeycodes-typescript-go/2.js:10:33
at one (/Users/andrew/Documents/GitHub/healeycodes-typescript-go/2.js:22:31)
at Object.<anonymous> (/Users/andrew/Documents/GitHub/healeycodes-typescript-go/2.js:34:1)
at Module._compile (node:internal/modules/cjs/loader:1830:14)
at Object..js (node:internal/modules/cjs/loader:1961:10)
at Module.load (node:internal/modules/cjs/loader:1553:32)
at Module._load (node:internal/modules/cjs/loader:1355:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:255:19)
at Module.executeUserEntryPoint [as runMain (node:internal/modules/run_main:154:5)
Node.js v24.15.0