4 ms·
It still seems like there would be an opportunity for a smarter language here. Like, the reason that `goto` appears here is because the language does not know h
by drostie 14y ago
It still seems like there would be an opportunity for a smarter language here. Like, the reason that `goto` appears here is because the language does not know how to clean up the mess that it's making, thus the code requires humans to jump back and forth between the top and bottom making sure that each cleanup section properly cleans up the corresponding mess above. This is true of both the `if` and `goto` code; and we could do this much more easily if those pieces of code were side-by-side.
- jacques_chester 14y agoYou might be interested in looking at the Common Lisp "Conditions" system, which allows code outside of the stack to provide error-handling logic. In general, though, I think it's more about presentation than the language per se. I've often felt that an IDE or editor that could selectively highlight or hide the different pathways through the code would be a very useful tool.
- rwmj 14y agoGCC and LLVM offer a "cleanup in this scope" attribute, so you can write: __attribute__((cleanup(free_indirect))) char *foo = strdup ("bar"); where free_indirect is a simple wrapper around free. It's used in the systemd code (making it GCC/LLVM-specific of course). http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html
- twoodfin 14y agoRAII style in C++ handles this kind of resource cleanup well, and it's the main feature I miss when programming in C.
- Garoof 14y agoSo, on a "some other language does like so" note, in D you might write it kind of like this: allocate_memory(); scope(failure) free_memory(); setup_interrupts(); scope(failure) teardown_interrupts(); setup_registers(); The scope keyword is for saying that if you get to this point, then do such and such at end of scope. scope(failure)/scope(success)/scope(exit), for exitting scope by exception/return/either. Is kind of nice.