logoalt Hacker News

kaashifyesterday at 10:36 PM4 repliesview on HN

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.


Replies

pjmlptoday at 5:49 AM

Only when doing stack allocations or reference counting, any other heap management strategy and RAII can't help as well.

For example, combining RAII with lock free data structures, which usually ends up in techniques like hazardous pointers instead.

mcintyre1994yesterday at 10:50 PM

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

show 1 reply
okigantoday at 1:14 AM

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!

show 2 replies
Rohansitoday at 2:52 AM

I mean, sure, but RAII (in C++, at least) is implemented the same way as this article: with a try...finally block!

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.