News for hackers. Stuff that matters.
Re:A Programming Idiom You've Never Heard Of by WalterBright on Wednesday January 04, 2012 @05:44AM (#3423594)
In D, this is done with scope guard (or you can use destructors):

    f = open("test.txt");
    scope(exit) f.close();
    print(f.read());
The scope(exit) causes the following statement to be executed at the exit of the scope, regardless of how it leaves (return, goto, fall through, throw, etc.).

The advantage over try-finally is the 'finally' statement is associated with the open, rather than some arbitrary distance away.

Re:A Programming Idiom You've Never Heard Of by masklinn on Wednesday January 04, 2012 @05:57AM (#3423622)
> In D, this is done with scope guard

Which is nowhere near as good, as the closing operation has to be setup explicitly. Go has the same issue with `defer`.

Re:A Programming Idiom You've Never Heard Of by WalterBright on Thursday January 05, 2012 @03:16AM (#3427928)
The various tradeoffs between try-finally, RAII, and scope guard is discussed here:

http://www.d-programming-language.org/exception-safe.html

Scope guard has a number of distinct advantages.