-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disposable #43490
Comments
Dart currently uses either |
The idea is in the standard interface that would serve as a marker that the object that implements it needs to be released. This will provide a convenient check for other code that will work with these classes.
Instead of this:
|
For That suggests that Dart object disposal is not homogeneous enough to be supported by a single interface. Having two interfaces ( Dart has first class functions, so I'd prefer composition to inheritance. Instead of making objects That sound like it would be something like: abstract class Disposer<T, R> {
Disposer(T value, R dispose()) = _Disposer<T, R>;
T get value;
R dispose();
}
class _Disposer<T> implements Disposable<T> {
final T value;
final R Function() _dispose;
_Disposable(this.value, this._dispose);
R dispose() => _dispose();
} Then, if you want to dispose something, you just wrap it in a extension SubscriptionDisposer<T> on StreamSubcription<T> {
Disposer<StreamSubscription<T>, Future<void>> get disposer => Disposer(this, this.cancel);
} |
We can use generic Disposable.
|
A generic disposable means that generic disposal handling code cannot assume that the return value is a |
I'm understood, thank you. |
By the way In C# we now can use async dispose method as well using IAsyncDisposable. So we can do same thing in dart too by providing multiple Disposable interfaces for both sync and async dispose operations. abstract class Disposable {
void dispose();
}
abstract class AsyncDisposable {
Future<void> disposeAsync();
} Also, I would rather have non-async Disposable interface rather than having to unsafely disposing dynamic or dealing with 3rd party disposable interfaces. |
@lrhn To be honest your solution looks also kind of complicated/complex for me at least😄 What about |
If the And it's bad API design to return a |
Most of the time we need to just kick-start the disposal and assume the resources will eventually get free. How often do you see await streamController.close() instead of just streamController.close() ? When using Flutter, often we even cannot await because we mostly free resources in widget state's All too often we have this sync interface copied from one app to another and get re-declared in libs: abstract class Disposable {
void dispose();
} Maybe we should just get it into the SDK? It alone would cover needs of most people reading this issue. Next, we may use the fact that abstract class AsyncDisposable extends Disposable {
@override
Future<void> dispose();
} So objects may expose
With such inheritance and a single method name, seems like it would make things more clear. |
The problem here is that we can't even add Also, making |
why do we need to unify the AsyncDisposable and Disposable? I propose to scope out the sync&async dispose unification for an another issue, and start benefiting from having a |
If they were "unified", the other direction would likely work better: // Don't implement this class, use one of the ones below.
abstract interface class PotentiallyAsyncDisposable {
FutureOr<void> cancel();
}
abstract interface class AsyncDisposable implements PotentiallyAsyncDisposable {
Future<void> cancel();
}
abstract interface class Disposable implements PotentiallyAsyncDisposable {
void cancel();
} Then a You need an |
not quite sure why you would even try to do this. maybe this is more a Flutter topic than Dart. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Problem is: What if the constructor or |
@MarvinHannott Frankly, i dont see why we cant have both, a This all makes me think of the whole serializable thing, which is very similar in concept, and could possibly benefit from the same logic |
Actually I don't see where the problem is having an abstract class Disposable{
}
abstract class DisposableSync implements Disposable {
{
void dispose();
}
abstract class DisposableAsync implements Disposable{
Future<void> dispose()
}
abstract class Cancelable implements Disposable{
void cancel();
} That would allow to write universal disposer functions that can deal with any I don't understand why introducing such interfaces would be a general breaking change. It only would break code that declares already classes with the same name. |
I agree, I don't mind having the interfaces be completely seperate. There is definently a migration that has to be done for Dart, then Flutter, then the pub.dev packages. Perhaps there can a lint that looks at your class methods to see if there's a parameter-less |
Add an abstract class Disposable that contains a disposable method. To standardize work with classes that need to be released.
The text was updated successfully, but these errors were encountered: