5 ms·
I have seen a claim that Ada now has or is getting something akin to C++'s destructors or Rust's Drop trait. What would Ada's version be called, and what would
by ncmncm 5y ago
I have seen a claim that Ada now has or is getting something akin to C++'s destructors or Rust's Drop trait. What would Ada's version be called, and what would it look like?
To me, the destructor is the single most distinctive and powerful feature of C++, adopting it is what made Rust viable, and lacking it makes Zig overwhelmingly less interesting than it might have been. If Ada got destructors, that would promote the language, in my view, from dead to potentially viable.
- atombender 5y agoAda's version of this is called controlled types. A type can have a finalizer method that gets called when a value needs to be destroyed.
- msla 5y agoIs it possible to precisely control when it gets called? If not, how predictable is it?
- atombender 5y agoYes, you have complete control over this. In short, it's a hybrid between manual and automatic memory management. Memory allocation is a complex subject in Ada, and I'm not familiar enough with it to explain all the nuances, but here [1] is a good discussion. [1] https://stackoverflow.com/questions/67131931/does-ada-deallocate-memory-automatically-under-some-circumstances https://stackoverflow.com/questions/67131931/does-ada-deallo...
- zozbot234 5y agoDoesn't the Ada.Finalization package (since Ada 2005) give you this anyway?
- deleted 5y ago[deleted]
- ncmncm 5y agoNo. To be useful it needs to be a core language feature.
- pyjarrett 5y ago`Ada.Finalization` is a core language feature, it's a package defined as part of the standard. Like many Ada features, RAII (via Controlled Types) is something you can opt into, such as if you need more than just record initialization.
- ncmncm 5y agoI am corrected.
- WastingMyTime89 5y agoIt is a core language feature. You don't understand what Ada packages are.
- okl 5y agoCore language feature and package are not necessarily the same thing. No reason to bump heads. Instead you could provide a link to the RM: http://www.ada-auth.org/standards/12rm/html/RM-7-6.html http://www.ada-auth.org/standards/12rm/html/RM-7-6.html
- pjmlp 5y agoLanguages like Ada provide hooks, for the runtime and compiler to collaborate together, because safety is more relavant than performance at all expenses. Any variable declared as controlled type will have their "destructor" called just like C++, and that is what matters. Like like RAII doesn't do anything, if the type is heap allocated and due to a bug not deleted or freed(), and someone has to write the destructor code, just like someone has to write the controlled type.
- msla 5y ago> Languages like Ada provide hooks, for the runtime and compiler to collaborate together, because safety is more relavant than performance at all expenses. Does Ada come with optional garbage collection, at least? Memory leaks are a big safety concern, perhaps the biggest in practical code, and IIRC Ada still doesn't have GC "by default" for some unimaginable reason.
- awestroke 5y agoWhat 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.
- llimllib 5y agoZig's thread on the topic is interesting reading - it has gone from open to closed several times, and includes interesting examples from the stdlib where resources were not closed properly: https://github.com/ziglang/zig/issues/782 https://github.com/ziglang/zig/issues/782
- WalterBright 5y agoD has conventional destructors: https://dlang.org/spec/struct.html#struct-destructor https://dlang.org/spec/struct.html#struct-destructor