Support for "using" in function parameters #2628
-
I would like to propose support for the use of temporary proxy wrappers for parameters passed to functions. These proxies would then disposed of immediately AFTER the function had returned. This automatic disposal would be indicated by the inclusion of "using" keyword, as illustrated in the following examples: Example 1:
Example 2:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
This seems quite nifty, but I think that the one who created the object should be the one who disposes of it, otherwise this could cause a lot of confusion and obscure bugs. I also can't think of a situation where a method can guarantee no one will want to use the object after it's finished executing. |
Beta Was this translation helpful? Give feedback.
-
Personally, I'm strongly against adding syntactic sugar which is:
One the one side, you have the number of man-hours that you waste worldwide from people seeing this syntax that they've never seen before, and having to figure out / look up what it means. On the other side, you have the reduced time to type a few characters, and potentially reduced time grokking what the code does if you're familiar with the syntax. I don't think this meets that bar at all. It's only 7 characters shorter, it's going to be very rarely used, and I think it reduces clarity. Also, the behaviour around Task-returning methods is not obvious. Take for example var foo = await Foo(using GetSomething()); Does the result of Is the result of var task = Foo(using GetSomething());
await task; What about this? When is the result of public Task Bar() => Foo(using GetSomething());
await Bar(); |
Beta Was this translation helpful? Give feedback.
-
Which is likely on the scale of milliseconds at most considering that the |
Beta Was this translation helpful? Give feedback.
-
If I'm correct, the TResult result;
using (var r = new Resource())
result = DoSomething(r);
DoAnother(result); becomes: DoAnother(DoSomething(using new Resource()); But this only benifits if you need to dispose the resource as eager as possible, which is uncommon. Otherwise, you can dispose the resource after everything on the result are done. |
Beta Was this translation helpful? Give feedback.
If I'm correct, the
using
proposal is actually saving a bit more for some situations:becomes:
But this only benifits if you need to dispose the resource as eager as possible, which is uncommon. Otherwise, you can dispose the resource after everything on the result are done.