On Monday, April 17, 2017 11:14:47 Shachar Shemesh via Digitalmars-d wrote: > scope(exit) is a much cleaner solution than a finally clause, but > not as clean as RAII. If the language supports RAII, how come > people are not using it?
Well, if you're talking managing memory, most folks just use the GC. And for the rest, I think that RAII does get used fairly frequently (e.g. std.stdio.File uses it), but largely, RAII is only going to get used when some resource is going to need to be passed around or managed in the same way in several places, because otherwise, it's simpler to just put a scope(exit) statement in there than to declare a type. And the programs that do use RAII are simply buggy in the cases where constructors don't clean up properly when an exception is thrown. For a lot of code, it really doesn't matter (e.g. they simply aren't at any real risk of an exception being thrown during construction), and even in code where it does matter, it could easily be missed, especially if exceptions aren't thrown often. Certainly, this topic comes up infrequently enough that I really don't think that all that many folks are using scope(exit) instead of RAII simply because of the bug with constructors. I'd guess that most folks aren't even aware that it's an issue. Now, I totally agree that https://issues.dlang.org/show_bug.cgi?id=14246 is a serious issue that needs to be fixed and that some software is going to have some major issues as long as it uses RAII and that bug exists. But because D does not rely as heavily on RAII as C++ does, and RAII works correctly in many cases (particularly if an exception is not thrown during construction), a lot of code can be written which happens to use RAII without being buggy or without the bug being caught. It's when you start relying heavily on RAII that you're more likely to notice it. If RAII were completely broken, I suspect that this would have been a higher priority and would have been fixed ages ago. But because RAII mostly works, this problem has managed to go sufficiently under the radar that it's never become as high a priority as it arguably should be, and it hasn't yet been fixed like it should have been. - Jonathan M Davis
