-
Notifications
You must be signed in to change notification settings - Fork 754
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
Enhancement Add DisposeWith #2178
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace System.Reactive.Disposables; | ||
|
||
/// <summary> | ||
/// Extension methods associated with the IDisposable interface. | ||
/// </summary> | ||
public static class DisposableMixins | ||
{ | ||
/// <summary> | ||
/// Ensures the provided disposable is disposed with the specified ICollection of IDisposable./>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the disposable.</typeparam> | ||
/// <param name="item">The disposable we are going to want to be disposed by the disposable collection.</param> | ||
/// <param name="disposableCollection">The composite disposable.</param> | ||
/// <returns> | ||
/// The disposable. | ||
/// </returns> | ||
/// <exception cref="System.ArgumentNullException">compositeDisposable</exception> | ||
public static T DisposeWith<T>(this T item, ICollection<IDisposable> disposableCollection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm slightly baffled now that I look at this. It appears to be For example, this seems to let me write the following: List<IDisposable> disposables = new();
someDisposable.DisposeWith(disposables); This is, as far as I can tell, exactly equivalent to: List<IDisposable> disposables = new();
disposables.Add(someDisposable); This If there were something constraining the someDisposable.DisposeWith(compositeDisposable); than this: compositeDisposable.Add(someDisposable); Moreover, at that point it no longer works for anything other than It looks like a basic presumption behind this method is that it's inconceivable that anything implementing I suppose it might be possible to constrain the second argument too, e.g. something like: public static TResource DisposeWith<TResource, TCollection>(this T item, ICollection<IDisposable> disposableCollection)
where TResource : IDisposable
where TCollection : IDisposable That still makes a fairly big assumption: that the collection type will dispose all of its contents. But I don't think that's necessarily true. If the collection type specifically implements So here's how you'd actually want to constrain the collection type to be reasonably confident that
I don't believe it's possible to express that particular set of constraints in C#. So either you make this method so narrow that it is really just another name for So, before we draw any conclusions from this analysis, do you think I've missed anything in the above? |
||
where T : IDisposable | ||
{ | ||
if (disposableCollection == null) | ||
{ | ||
throw new ArgumentNullException(nameof(disposableCollection)); | ||
} | ||
|
||
disposableCollection.Add(item); | ||
return item; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking through Rx at all the other types that define extension methods, they tend to end in
Extensions
. E.g.,TaskObservableExtensions
,ObservableExtensions
,VirtualTimeSchedulerExtensions
.So far we haven't used the terminology mixin at all.
Do you think this is a fundamentally different sort of thing from the existing things that use the
Extensions
suffix? If this has a different-looking name from other similar things, and if that signifies an important conceptual difference, then that's reasonable.But if there is such a difference, I'm not sure what it is. Is there one? Or is this more of a naming preference thing? If so, how would you feel about aligning with existing conventions in this library here?