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: > > On 8/3/2013 12:00 PM, JS wrote: > > >What I really don't get it is why people think that just because they > > >won't use such a feature then it must be useless to everyone else. > > > > You could provide supporting evidence by examining every use of > > switch in the dmd, phobos, and druntime source code, and see what > > percentage of those would benefit from your proposal. > > > > 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. The only thing that's really missing from C++ that D has that you might use in place of scope is finally. 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. 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). 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). - Jonathan M Davis