Re:Zig, Skia, Clojure, Geometry and the Japanese TV Show: IC...
by Arnavion on Thursday July 15, 2021 @07:49PM
(#27851489)
They certainly have their problems. They're functions that get invoked without any code to explicitly invoke them. You have to consider whether a destructor can be invoked on a partially constructed object. You have to consider whether a destructor can be invoked more than once. You have to consider providing the ability to not call the destructor for performance. etc etc
It's understandable why a language trying to be close to C in spirit does not have them.
Re:Zig, Skia, Clojure, Geometry and the Japanese TV Show: IC...
by ghoward on Thursday July 15, 2021 @09:33PM
(#27852181)
Yes, those are all problems with destructors, but they are well understood. However, in a language I am currently creating, calling a destructor on a partial object is impossible because the object is created in "one" step. In other words, constructors themselves don't have access to the object in a partially created state.
It does make things much easier.
Re:Zig, Skia, Clojure, Geometry and the Japanese TV Show: IC...
by hnakamur on Thursday July 15, 2021 @08:13PM
(#27851696)
I agree. For example, most of Rust libraries uses the drop handler (something like desctructors) to close a file descriptor.
However it has problems since in async model closing is also async and it may return an error.
So in tokio-uring explicit async close will be provided.
https://github.com/tokio-rs/tokio-uring/pull/1/files#diff-3d...
And you can call close like: file.close().await.unwrap(); https://github.com/tokio-rs/tokio-uring/pull/17/files#diff-b...
I think people have a bias against destructors because of C++, but they really are pretty nice. Speaking as a guy that uses C instead of C++, destructors are the one thing I would add to C.