-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Proposal: Allow using
statement to structurally match IDisposable
on ref structs (16.3, Core 3)
#1623
Comments
I would also like it if we could allow an appropriately matching extension method. It seems reasonable to be able to embueue a type you don't own with RAII semantics if that makes sense for your domain. Just my 2c :) |
I like it, but I would suggest that perhaps we should consider extension methods right from the start (no matter what, if anything, we do about |
Ha! Beat you @gafter :) |
Not by much; we both submitted at 3:47. |
Dupe of #93 😉 |
I think we have too many special rules inherited from the days before we had extension methods, params, etc. I think we should just do ordinary overload resolution, and permit any method that can be called with the arguments to be used. Accessibility checked as usual, not requiring public. Allow extensions. In general, treat it just like any method call that happens to have zero arguments. We would need LDM buy-in for it. |
That should read: "except that if the type not does have an implicit conversion to Correct? The compiler should only apply the structural typing if the type isn't |
I think so. Does "structually matches" also include extension methods, or do we need to explicitly mention that? |
Just my thoughts but the existing Perhaps the entire mechanics of |
@HaloFour Correct. |
Just a question: what would be the benefit of doing this if extensions methods were to be ignored? I'm curious. |
@andre-ss6 Besides consistency with the rest of the language, it would also be a stepping stone to enable |
One concrete benefit: allows |
Asking a stupid question: if this proposal comes true, what is the point of implementing |
So that third-party code can safely dispose of your object... at least until shapes and |
What if there are two extension classes in scope that both provide |
@markrendle The same thing that happens with conflicting GetAwaiter() extension methods and In particular: sharplab |
@markrendle
This same situation can arise with |
I'd be curious to see what code would be affected by this that wasn't already buggy. Sounds like a big code smell. |
We just need Shapes. |
And Higher Kinded Polymorphism. Just imagine what a beautiful world it would be where c# supports HKTs 😝 |
Now that |
using
statement to structurally match IDisposable
using
statement to structurally match IDisposable
on ref structs
Also, given the following extension method public static ConfiguredCancelableAsyncEnumerable<T>.Enumerator ConfigureAwait<T>(this IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext) using the await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
{
// ...
} when
It'd be a pity to have to resort to For this to work as intended, Also tagging @stephentoub given this discussion refers to the corefx types introduced for configuring async enumerables. |
Isn't that the plan now / already implemented @jcouv? |
@bartdesmet Yes, we've arrived at that same conclusion (when @stephentoub implemented methods like For reference (if you want to look at some test cases), here's the PR for doing that in async constructs. |
Thanks, this works now as expected, and I've converted the LINQ code to use |
using
statement to structurally match IDisposable
on ref structsusing
statement to structurally match IDisposable
on ref structs (16.3, Core 3)
So, is this done and dusted? If so, can it be closed? |
C# 8 hasn't shipped yet, so this should remain open until it has |
Is the "extension-based foreach" rejected indefinitely or it's just cut from this release? |
Can this be closed, now that C# 8 has shipped? |
No, we keep issues open until a spec has been merged. |
But hasn't it? |
No. That adds a proposal ( |
Currently the language states that any type used in an
using
statement must have an implicit conversion toIDisposable
. This proposal is allow any type which structurally matchesIDisposable
(has a public void-returning non-generic instance method taking no arguments) to be used in ausing
statement.The semantics of this construct are the same as a regular
using
, except that if the type does not have an implicit conversion toIDisposable
, there is a check to see if it structurally matchesIDisposable
. If so, instead of theIDisposable.Dispose
call, there is a call to the structuralDispose
method.At the moment, extension methods are not considered, like
foreach
statements. If we were to add extension method support toGetEnumerator
forforeach
, I propose we also add it forusing
.[jcouv update:] This feature was restricted to ref structs due to backwards compatibility reasons. Also, the part about considering extension methods was split out of this feature, and it was not implemented in dev16.0 milestone.
Full proposal in https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/using.md
LDM history:
The text was updated successfully, but these errors were encountered: