Automatic Disposing #1922
Replies: 5 comments
-
As proposed, this is a breaking change and will almost certainly not be accepted. #1703 proposes the inverse. |
Beta Was this translation helpful? Give feedback.
-
If this were possible there would be no need for a Garbage Collector, and Rust would be a far simpler language. Take a look at RAII in Rust for the complexities involved in statically working out when a resource is no longer in use. In short this is impossible. The idea of a using statement is that the programmer says "Trust me. I'm not going to use this any more, and if I do I'll get an exception". For example: \\Assembly 1
public object GetThing()
{
//You have to look at the implementation of Return to know whether DisposableObject is returned by this method.
return Return(new DisposableObject());
}
\\Assembly 2 - the compiler can't see whats happening inside this method. It can only see the public API
public object Return(object obj) => return obj; |
Beta Was this translation helpful? Give feedback.
-
I don't like this because it increases maintenance complexity for the developer. It means I need to read and understand an entire method, and understand which of my variables are You may consider explicitness to be overly-verbose, but it makes things much more readable, and it's pretty rare to have a huge number of disposables in a single method so the cost isn't huge. |
Beta Was this translation helpful? Give feedback.
-
@TonyValenti The analysis required for proper escape analysis is complex, plus this would be a breaking change. Both of these mean that the chances of this happening are extremely low. But, check out both #1398 (Champion "defer statement") and #1174 (Champion "Implicitly scoped using statement) for two suggestions that improve on the current using syntax. |
Beta Was this translation helpful? Give feedback.
-
I think the feature/concept is very powerful, helpful and important. It's one of the biggest features that made C++ better than C. I also thought about it a lot... And I greatly agree with others.
@YairHalberstadt kind of mentions, the ownership problem. It is something, they had to fight with in C++ and got partially solved by moves. So... such new non-unmanaged, non-class thing would have to deal with default assignment behavior of copying (for instance, by disallowing it), add some sort of move behavior or let types define their copy as move... and/or ask compiler to prevent use-after-move or use-after-copy(move). All of these changes, together, are quite expensive. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
I just finished reading the proposal for async streams and there were a few things that stood out to me:
I wanted to give a suggestion as it relates to IDisposable.
Whenever an IDisposable is created, the compiler should track where it gets created and used and should automatically Dispose() of it if it can determine that it is no longer necessary.
For example:
Beta Was this translation helpful? Give feedback.
All reactions