Consider providing a keyword to leak variable scopes. #2130
Replies: 15 comments
-
It's a little tricky to see what you are asking for. Instead of using a picture, could you write a minimal example, showing what you currently have to do, and what you would like to have added to the language. A little bit of explanation about the semantics of the feature, advantages, disadvantages, alternatives etc. always does help, but is not critical. |
Beta Was this translation helpful? Give feedback.
-
You're free to declare variables in the outside scope and then assign them within the lambda. |
Beta Was this translation helpful? Give feedback.
-
That is correct. However think of I'll have a look at the package though, thanks. |
Beta Was this translation helpful? Give feedback.
-
I purposely used an image here, to point out how much more convenient the later syntax would be, if it were to exist the way i would imagine it. |
Beta Was this translation helpful? Give feedback.
-
If that is true that package description is true, that perfectly solves my requirement, thanks - however it would be great if this were a language feature too, so everyone could benefit from it. |
Beta Was this translation helpful? Give feedback.
-
@taori Nonetheless adding a minimal text example does make it easier for most people to quickly understand what you want. |
Beta Was this translation helpful? Give feedback.
-
Fair enough. I'll add some sample code. Thanks for the suggestion. |
Beta Was this translation helpful? Give feedback.
-
The language feature is that everything is awaitable, as long as someone provides the correct API. The language only cares that it can resolve a There was a proposal to add these APIs to the BCL, but unfortunately that was closed: https://github.com/dotnet/corefx/issues/16010 |
Beta Was this translation helpful? Give feedback.
-
Yeah. i found a blog mentioning this a year back or so. Too bad i never came up with using it like this. That sure is a damn amazing way of doing Task.WhenAll. Very grateful for you sharing this with me :) Very sad it didn't make it into BCL |
Beta Was this translation helpful? Give feedback.
-
There's also a proposal to add awaiting a tuple of tasks directly to C#: #380 (though it doesn't seem likely it will be accepted anytime soon). |
Beta Was this translation helpful? Give feedback.
-
Wouldn’t Declaration Expressions (#595) cover this particular case? |
Beta Was this translation helpful? Give feedback.
-
The best keyword to re-use for leaking would be https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unsafe |
Beta Was this translation helpful? Give feedback.
-
Actually it very much looks like this request would be fulfilled if #595 were implemented, yes. |
Beta Was this translation helpful? Give feedback.
-
When you have this var func = (Action<int>) x => { declare var y = x; }
if(cool) func(3);
Console.WriteLine(y); How are you going to guarantee that Something like this could work but i'm not a fan :) var func = (Action<int>) x => { declare var y = x default 0; } Also leak by how much? {
{
{
declare var x = ...;
}
}
}
// is x accessible here? |
Beta Was this translation helpful? Give feedback.
-
To be honest that is a case i did not even think of. While i think the feature would be nice to have, i am not quite sure how the feature would even work if you can't make sure the expression which would declare a variable is called before the code which uses it.
I would say no. it should only link to the same level the statement is declared on. |
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.
-
I've got some code which depends on the result of 3 web requests. The following code could be much more brief if there was a way to let variables leak.
if there were a keyword with that functionality i could turn all that code into something like this:

Current
Suggestion
Using Task.WhenAll always feels rather awkward if you need to combine the results of parallel requests - with a keyword to leak those varibles to the outer scope, that would be a non issue.
Beta Was this translation helpful? Give feedback.
All reactions