On Saturday, August 03, 2013 14:25:24 H. S. Teoh wrote:
> On Sat, Aug 03, 2013 at 01:33:44PM -0700, Jonathan M Davis wrote:
> > The only thing that's really missing from C++ that D has that you
> > might use in place of scope is finally.
> 
> Truth be told, I think scope trumps finally because it localizes the
> cleanup statement to the initialization. Sprinkling related code across
> two different places is more fragile and bug-prone.

Yes. There are lots of places where scope(exit) is far better than finally. 
It's just that aside from scope statements, the only related feature that C++ 
lacks is finally (and scope(exit) uses finally underneath the hood).

> > C++ has RAII just like D does.  There's a much bigger difference when
> > comparing against languages which don't have RAII (such as Java or
> > C#), but with C++ has RAII.
> 
> Yeah, I think I mostly had C in mind when I wrote what I did. :)

Yeah. The lack of RAII and scope in C is truly painful. I'm very glad that 
I've never had to write much pure C code.

> > I definitely use scope from time to time, but I don't know how
> > frequently I use it in comparison to how often I should use it. I was
> > actually using scope(success) more until someone pointed out that
> > there's an efficiency hit with that as it was putting try-catch blocks
> > in places where there wouldn't have been before (as opposed to
> > scope(exit) or scope(failure) where you'd be using try-catch blocks if
> > you weren't using scope).
> 
> [...]
> 
> Hmm, I never thought of that. So that limits the use cases of scope
> guards even more. :-P

Yeah. For instance, I'd made it so that RedBlackTree was using scope(success) 
for mutating its length in a few places, which helped make the code cleaner 
(because you didn't have to worry about incrementing the length at each of the 
various return statements), but that introduced try-catch blocks where there 
otherwise wouldn't have been any, and that code definitely needs to be as 
efficient as possible, so the scope(success) statements were removed from 
there. 
Other code wouldn't care as much, but for something like RedBlackTree, it 
really matters. It is a bit of a shame though, since it made the code cleaner.

- Jonathan M Davis

Reply via email to