6 ms·
D and Rust are the only languages that have since adopted the destructor.
by ncmncm 6y ago
D and Rust are the only languages that have since adopted the destructor.
- richardwhiuk 6y agoObjective C also has destructors.
- ncmncm 6y agoNot the same, and overwhelmingly less useful.
- setpatchaddress 6y agodealloc is pretty much the same thing, and it can be used in the same way. It's not "useful" in the C++ RAII sense because: - the standard library design -- every object is refcounted and any code that references the object can add it to an autorelease pool, so you can't safely predict when dealloc will be invoked - Objective C doesn't support stack-based objects
- richardwhiuk 6y agoYou can do stuff: @autoreleasepool { RaiObj* obj = [RaiObj new]; [obj autorelease]; //... stuff } will de-allocate at the end of the block if nothing else hold onto it, but it's obviously less neat.
- bestouff 6y agoI tend to see Rust as "only the good parts of modern C++, and then some".
- aldanor 6y agoAnd also: "Minus all the OOP, and then some."
- saagarjha 6y agoModern C++ generally doesn’t use OOP all that much anyways. However, <algorithm> and <type_traits> in Rust’s standard library would be quite nice to have.
- pjmlp 6y agoOther than the classes used to implement all those STL concepts.
- johannes1234321 6y agoTypes are being used to create those things. But it's not OOP as many people understand it. (Inheritance, everything via method calls etc.)
- pjmlp 6y agoPeople should spend more time learning what OOP actually is all about, hint it isn't just MFC and Qt.
- gpderetta 6y agoif STL is OO then everything is OO.
- pjmlp 6y agoThen remove the classes, static inheritance via template arguments, method dispatch via type parameters (very CLOS like), class member functions, iostreams, ..... Plenty of SIGPLAN papers to one educate themselves about what OOP is all about. OOP is not MFC alone, apparently those that learn languages on the trenches without referring to them, never get to appreciate the wealth of information we have available. Not surprising, given how many misunderstand "whatever my compiler does" for what ISO actually says.
- pjmlp 6y agoAlongside Object Pascal back in the old MS-DOS and Apple days, Ada 95, Swift, Python, and a couple of others that I won't now bother to search for on my papers archive.
- tonyarkles 6y agoHere’s the crucial difference between Python destructors and C++ destructors: C++ destructors run as soon as the object goes out of scope, deterministically. Python destructors run down the road, during the next GC cycle. As an example of why this matters, I’ve got a class called TimeCapture in a project. If I want to instrument how long a block takes (full function, inside of an if, etc) I can add `TimeCapture tc(“my-metric-name”);` to the block and know that the destructor on that object will be called precisely at the end of that block (capturing the end time of the block) I don’t know the rules for Swift or Ada or OP, but I’m guessing they’ll vary.
- kubav 6y agoTo implement scope guard you can use with statement and implement __enter__, __exit__ methods. It is not real RAII as object is not destroyed on exit but it is still quite useful. http://effbot.org/zone/python-with-statement.htm http://effbot.org/zone/python-with-statement.htm
- achn 6y agoHow do multiple destructors get sequenced? Last in, first out? Thanks
- masklinn 6y ago> Here’s the crucial difference between Python destructors and C++ destructors: C++ destructors run as soon as the object goes out of scope, deterministically. Python destructors run down the road, during the next GC cycle. Not true. They run when the object is deallocated. In CPython if the object is not involved in or referenced from a cycle, that’s as soon as it goes out of scope: the ref count goes to 0 and the object gets deallocated. This is trivial to check: create a structure with a noisy __del__ and create an instance from a function, call the function, del will run. The GC has no reason to run at this point, there’s no allocation requiring any sort of reclamation, but if you don’t trust that you can just `gc.disable()` and observe… the exact same behaviour.
- sacado2 6y agoDon't forget good old Ada.
- ncmncm 6y agoAda does not have destructors or anything analogous.
- Jtsummers 6y agohttps://www.adaic.org/resources/add_content/docs/95style/html/sec_9/9-2-3.html https://www.adaic.org/resources/add_content/docs/95style/htm... "Finalize" on objects of type "Ada.Finalization.Controlled".
- ncmncm 6y agoNope. Key words there are "should call".
- Jtsummers 6y ago> Nope. Key words there are "should call". I mean, yes, those words are in there, but I'm responding to your words: > Ada does not have destructors or anything analogous. Which are factually wrong. Finalize is called automatically (per the link). And they are exactly what you say Ada lacks, analogues of destructors. Your complaint, now, is that Finalize and Initialize don't automatically call the same functions in the parent. That's a separate issue, and doesn't demonstrate that Ada doesn't have this thing (a variant of destructors) which it very clearly has.
- ncmncm 6y agoIt fails to implement the aspect of destructors that makes them an architecturally important feature.
- ChrisMarshallNY 6y agoSwift has it. It’s called `dealloc()`. Since the language is fairly tight, and doesn’t have real stack-unwinding exceptions, there’s not a whole lot of utility in it. Apple seems to think that anything that you put in dealloc should end up in the system (like unsubscribing from notifications). C++ is a dangerous, powerhouse systems language. I think it really needs things like destructors, and I’m glad to see its continuing development.
- saagarjha 6y agodeinit, and if they’re from NotificationCenter you haven’t had to do that for a while and for KVO if you’re using the block syntax (you should!) you basically have an RAII “observation” that you control the lifetime of (often you assign it as a property of the object being observed, so it automatically gets cleaned up when the object goes out of scope).
- ChrisMarshallNY 6y agoYou're right (shows how often I use it, and I write Swift every day). Apple is amping up KVO, in general, which I like.
- threatripper 6y agoPython also has destructors (def __del__(self):). They are called at whatever time and whichever order the GC gets around to it.
- saurik 6y agoThe Python people don't want you to rely on this--which I only learned many years later and it frankly ruined Python for me (are least in the abstract, as "practically" I can ignore this for my projects), as I consider "deterministic finalization" to be a critical language feature--but CPython uses reference counting and thereby has predictable deconstruction.
- ben509 6y agoCPython uses reference counting and gc. It's the worst of both worlds: the expense of refcounting, and faux determinism until some innocuous code change causes you to hang on to a reference.
- klyrs 6y agoRefcounting needs a mechanism for cycle breaking, or it's necessarily leaky. This isn't the worst of both worlds; it's simply a consequence of refcounting. The same issue occurs in c++ if you're sloppy with shared_ptr. If you know what you're doing, you can break cycles with weak references in either language. Where Python has diapers, c++ has footguns aplenty.
- int_19h 6y agoThe problem with Python isn't that it has reference cycles. It's that it has a tracing GC that tries to automagically fix those cycles for you. So in C++, if you get a cycle, the destructors do not run at all - which is pretty noticeable. In Python, they run "sometime later", so programmers are less inclined to fix the cycles (indeed, many aren't even aware that it's a problem). But if you provide a non-deterministic mechanism on top of refcounting to break the cycles, and destructors aren't consistently deterministic already, why even bother with refcounting at all?
- johannes1234321 6y agoAlso PHP.
- ncmncm 6y agoSorry, no.
- johannes1234321 6y ago"Sorry No" as in "PHP is a stupid language, stay away!" Which is a point I won't argue. Or in the "PHP has no destructors"? - in that: PHP's object model is an "interesting" mixture of Java and C++. In regards to C++ it has detemrinsitric destructors, which enable RAII. The only complication is that all objects are reference counted (like shared_ptr in C++) and one can easily lose ownership from where one expects it to be. However if you keep an eye on ownership you can rely on reference count being decreased on function exit (be it regular exit or exception) and the destructor being called, in LIFO order as one expects. This isn't used much in PHP as you don't have to manage resources as often as with other languages/domains (in short lived request world one doesn't care whether a database connection is destroyed on function end or only on request shutdown, memory is handled automatically, etc.)