On Sat, Aug 03, 2013 at 01:33:44PM -0700, Jonathan M Davis wrote: > On Saturday, August 03, 2013 12:35:22 H. S. Teoh wrote: > > On Sat, Aug 03, 2013 at 12:22:53PM -0700, Walter Bright wrote: [...] > > > Consider, for example, the scope guard statement in D. It is > > > extremely useful - but it is an unusual form (doesn't exist in > > > other languages) and programmers just don't think in those terms. > > > Andrei & I constantly have to work at 'selling' the benefits of > > > it. It still hasn't really caught on. > > > > At least it has caught on with me. :) After learning about scope > > guards in D, I've been plagued with persistent thoughts of "I wish I > > could use a scope guard here!" every time I work with C/C++ code. > > > > Ironically, though, D's superior design has pretty much eliminated > > the need for scope guards except in a few rare cases. :-P They used > > to be still useful for things like closing files at the end of the > > block, but struct dtors have pretty much eliminated that use case as > > well. > > I'm surprised that you'd miss scope in C++ and yet not use it in D.
Well, I miss it more in C than C++, but C++'s manual memory management does make scope rather attractive. The same goes for D, actually, but that's largely alleviated due to the built-in GC. > 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. > 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. :) > In general, I think that it comes down to a question of whether RAII > or scope(exit) is more appropriate, and RAII works better in common > cases (such as closing files or releasing a mutex), wheras scope(exit) > works better in cases which aren't common (since it doesn't make sense > to create types just for those cases). RAII also makes more sense in > cases where you essentially need a reference count before doing the > action rather than just when exiting the scope (e.g. closing files is > also a good example of that; a File struct is more flexible than using > scope(exit) to close the file). Yeah, with RAII, much of the use cases of scope is eliminated. I'm mainly working with C at my job right now, which is why I miss scope so much. C is just so tedious to use due to the amount of manual baby-sitting required to make things work properly. I still miss scope in C++ because of the lack of a GC, but otherwise, even C++ doesn't have that many use cases for scope left. > 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 T -- My program has no bugs! Only unintentional features...