Live data from Hacker News

Adding Go's Defer to the TypeScript Compiler

healeycodes.com

1–10 of 31 posts

Re: Adding Go's Defer to the TypeScript Compiler

#3

Article describes try/finally as a hack to get the effect of defer, but it looks to me like it's the other way around? Try/finally is more traditional, in one form or another.

It's also necessary here because TypeScript has exceptions and you'd expect your `defer`s to execute even when an exception is thrown.

Re: Adding Go's Defer to the TypeScript Compiler

#4

Article describes try/finally as a hack to get the effect of defer, but it looks to me like it's the other way around? Try/finally is more traditional, in one form or another.

If the idea of computers is that they remember stuff for you and do stuff for you, then both try/finally and defer seem like hacks to work around not having RAII like e.g. Rust or C++ where resources are closed/disposed of/deallocated automatically.

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.

Re: Adding Go's Defer to the TypeScript Compiler

#5
post #4

Article describes try/finally as a hack to get the effect of defer, but it looks to me like it's the other way around? Try/finally is more traditional, in one form or another.

If the idea of computers is that they remember stuff for you and do stuff for you, then both try/finally and defer seem like hacks to work around not having RAII like e.g. Rust or C++ where resources are closed/disposed of/deallocated automatically. 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 need…

JS has the using keyword for this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re: Adding Go's Defer to the TypeScript Compiler

#6
post #4

Earlier quoted context omitted.

If the idea of computers is that they remember stuff for you and do stuff for you, then both try/finally and defer seem like hacks to work around not having RAII like e.g. Rust or C++ where resources are closed/disposed of/deallocated automatically. 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 need…

JS has the using keyword for this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

What if you forget to use that and just use let or const? It is better than plain try-finally or defer because you don't have to remember how to dispose of the resources but in terms of remember to dispose of the resource at all, I don't think that is all that different from Python's with or Java's try-with-resources. You can just forget to use using, with, try-with-resources.

There isn't any way to forget to drop a resource if you have RAII.

Re: Adding Go's Defer to the TypeScript Compiler

#8
post #4

Article describes try/finally as a hack to get the effect of defer, but it looks to me like it's the other way around? Try/finally is more traditional, in one form or another.

If the idea of computers is that they remember stuff for you and do stuff for you, then both try/finally and defer seem like hacks to work around not having RAII like e.g. Rust or C++ where resources are closed/disposed of/deallocated automatically. 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 need…

it's mesmerizing that the fantastic ergonomics of RAII have not propagated to other languages.

It's way easier to use, much clear, less typing, more predictable runtime behavior.

RAII... terrible name, great concept!

Re: Adding Go's Defer to the TypeScript Compiler

#10
post #6

Earlier quoted context omitted.

JS has the using keyword for this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

What if you forget to use that and just use let or const? It is better than plain try-finally or defer because you don't have to remember how to dispose of the resources but in terms of remember to dispose of the resource at all, I don't think that is all that different from Python's with or Java's try-with-resources. You can just forget to use using, with, try-with-resources. There isn't any way to forget to drop a…

Yes, but you also have this problem with `defer`.
Post reply on HN