7 ms·
> Invariants are functions that run at the start and end of every public member function these are just runtime assertions EDIT: how am i getting downvoted fo
by almostgotcaught 1y ago
> Invariants are functions that run at the start and end of every public member function
these are just runtime assertions
EDIT: how am i getting downvoted for copy-pasting literally what the article verifies?
- jayd16 1y agoI think there's something to be said about them running automatically that is lost when you say they're just asserts.
- almostgotcaught 1y agoi don't get it - if do int foo(int a) { assert(a > 5); int b = a * 10; assert(b > 50); return b; } do you think those asserts don't "run automatically"?
- gblargg 1y agoYou define the invariants once for the class and they are run around every public function. Done manually you'd probably use a helper object that calls the invariants in its constructor and destructor (have to handle exceptions) that you have to add to every public function's definition.
- readthenotes1 1y agoMaybe it's the editorial "just"? Like: software programs can't be that difficult to create properly because they are just 1s and 0s.
- johnisgood 1y agoThis is not the first time someone getting down-voted for using the word "just". I do not know if this really is warranted, however.
- LorenDB 1y agoYes, but they are guaranteed to run at the beginning and end. C/C++ asserts need to handle any return path, whereas D has functionality to mark statements to run at the end of any return path while only being written once. See also the scope(exit) feature.
- almostgotcaught 1y agoYou can accomplish the same exact thing with https://en.cppreference.com/w/cpp/experimental/scope_exit.html https://en.cppreference.com/w/cpp/experimental/scope_exit.ht...
- WalterBright 1y agoThe idea for scope-exit came from Andrei Alexandrescu. See https://dlang.org/articles/exception-safe.html https://dlang.org/articles/exception-safe.html He demonstrated it with C++ templates, but the D one is far more straightforward.