6 ms·
What do you use destructors for?
by awestroke 5y ago
What do you use destructors for?
- tempodox 5y agoReleasing allocated resources automatically, for instance. There are many possible applications.
- ncmncm 5y agoEverything, really. They are the only bit of runtime automation ever invented, besides Garbage Collection, but manage any sort of resource, most trivially memory. Without, everything needs to be programmed by hand, everywhere, fallibly. With, libraries can do the work, invisibly and with absolute reliability.
- pjmlp 5y agoAbsolute reliability, well { raii_handle *my_type = new raii_handle(new type); .... } /// ooops Naturally it doesn't happen just like that, rather in 500 lines of code function in code that has already been through 10 contracting agencies, including some offshored ones.
- ncmncm 5y agoIgnoring libraries does not demonstrate anything about their reliability. { auto p = std::make_unique<type>; ... } /// no oops.
- pjmlp 5y ago{ auto p = new std::make_unique<type>; ... } /// oops again. The fact being the "reliability" is a good as the hundreds of developers that touched the code. Real reliabile features don't depend on their skills to actually work 100% of the time, and doing something like newing a RAII type would be a compiler error, but alas it isn't.
- ncmncm 5y agoAnybody can write crap code in any language, Ada very much included. So, the example is meaningless BS. What matters is whether it is easier to write crap that compiles than correct code, and whether the easiest sort of crap compiles. That is where there has been progress. If you never, ever need to write "new" anymore, why would you accidentally write it? The BS example above does not, in fact, compile, because make_unique<> is not a type. So, it illustrates the progress I cite.
- pjmlp 5y agoYes the example is wrong, I was just trying to make a point while copy-pasting on the phone. One thing Ada was designed for and C++ not, was to prevent crappy developers to mess up. Anyone that has reviewed C++ code from offshoring Asian companies is well aware of what I mean.
- ncmncm 5y agoYour point was wrong. Your example illustrated that your point was wrong. Bad code from dodgy, offshore outsourcing services may be in any language. Bad Ada code is no better than bad code in any other language. Bad Ada code could be worse if it often works by accident, where other code might have failed visibly and been rejected.
- MaxBarraclough 5y ago> Bad Ada code is no better than bad code in any other language. Ada's strictness allows it to catch many more errors at compile time than, say, C. > Bad Ada code could be worse if it often works by accident, where other code might have failed visibly and been rejected. This is backward. Errors will be more easily detected in Ada than in, say, C. C is full of footguns and undefined behaviour. Vanilla Ada still isn't actually a safe language, unfortunately, but as a matter of degree, it's much safer than C.
- Lucretia9 5y agoOh, so all my smart pointers I have written in Ada don't finalise when Finalize is called, by the runtime?
- pjmlp 5y agoThe example is about C++.
- Lucretia9 5y agoHe was complaining of no destructors in Ada, when there are.
- pjmlp 5y agoWhile asserting the reliability of RAII in C++, which only works out if you don't have devs that do stupid stuff like the example above. You might consider that example stupid and argue no one would ever write code like that. To which I would answer you haven't reviewed enough offshored C++ projects.
- bigbizisverywyz 5y agoWould maybe be nice if there was a static check in C++ to verify whether something was declared on the stack, then that could be used to eliminate some of the potential RAII mishaps.
- guenthert 5y agoWhat about Python's "with" statement and context managers? Or Common Lisp's "with-" macros? Or Java's try-with-resources? I'm sure many other programming languages have automatic exception-safe resource management constructs as well.
- ncmncm 5y ago"With" must appear in client code, much like "finalize" clauses in Java and in similar GC languages. That is the opposite of automation: everywhere you need cleanup, you have to repeat the incantation, and get it right, again, every time. The point of destructors is that they are wholly contained in the library. It takes no extra client code to run them, and they are exercised identically on every single use of the type, so are well tested. Identically the same code runs on a throw, so there is much less risk from usually poorly exercised failure cases.
- deleted 5y ago[deleted]
- nyanpasu64 5y agoFreeing memory, releasing locks, closing files (though destructors are less expressive than separate close methods because destructors can't return error codes, which is why I want linear types which can make calling a close() method mandatory), committing transactions (though some argue it should be an explicit function call)...
- pjmlp 5y agoIn .NET land there are Roslyn analysers that trigger a compiler error if you don't call using on a IDisposable type.
- bregma 5y agoImplementing RAII. It's the singlemost fundamental C++ idiom.
- pjmlp 5y agoIndeed, and although already possible in the very first C++ compilers, it is still foreign enough that many include it as part of modern C++.
- ncmncm 5y agoIt is utterly foundational to modern C++. You could not leave it out and even make sense.
- bluGill 5y agoIt is the foundation of C++. Even in pre-modern destructors were the heart even if we didn't use them as much as we do now with modern C++.
- ncmncm 5y agoWe have always relied on destructors fully as much as we do now. What is new is that we hardly ever need to write one, anymore, or even to declare one. That is a consequence of a more mature standard library. I wonder now whether Ada auto-generates destructors for types with a member that defines one. And whether its standard library is such as to make a need to code them yourself vanishingly rare.
- pjmlp 5y agoIt foundational to C++, period. Even in Turbo C++ 1.0 for MS-DOS released in 1992 or thereabouts. The fact that many still think it only came with C++11 or whatever "modern C++" is where the problem lies.
- WalterBright 5y agoEven in Zortech C++'s 1987 release!