Proposal: sequential and concurrent keywords for asynchronous code #2400
Replies: 9 comments
-
My main problem with this proposal is that it makes it much harder to understand what is actually happening in your code. If you have something like This is even worse for concurrent {
OperationOneAsync();
SyncOperation();
OperationTwoAsync();
} Or is that forbidden? If it is, how exactly does the Some other points:
|
Beta Was this translation helpful? Give feedback.
-
The idea is to make forbidden dependencies on the tasks result, when another method call with IAwaitable is above in the block. For example:
will be forbidden.
is ok. |
Beta Was this translation helpful? Give feedback.
-
It was the example, that I thought to use as a opposite, but I found it the least readable, and therefore used await and creation of list. The reason is that the main idea of the code is to call MakeAsync, but the complexity related to Enumerables hides the initial idea and makes the enumeration as the main idea. But for loop is one of the most common construct in the programming languages, and it doesn't take away the attention from the main operation, from my perspective.
It will look more complex with Linq. |
Beta Was this translation helpful? Give feedback.
-
The regular async/await will be still available for these cases. |
Beta Was this translation helpful? Give feedback.
-
I think this is not good.
Does it save keystrokes? Yes.
Does it make the code clearer? No. In fact, it hides useful details.
Do I wish that C# async had been implemented differently using the lessons learned F# and C++? Of course. But unless we bite the bullet and add something like a new keyword async2 (or whatever), or simply change the implementation going forward and potentially introduce a keyword async_v1 for forced backwards compatibility, obscuring the details of the current async plumbing only serves to further entrench the legacy C# async design decisions.
Burying those details behind more succinct keywords without improving the fundamentals doesn't provide any great, worthwhile benefit.
Regards,
|
Beta Was this translation helpful? Give feedback.
-
Not by much: var tasks = Enumerable.Range(0, 100).Select(i => MakeAsync(i))
.Append(AndSomethingElseOne())
.Append(AndSomethingElseTwo());
await Task.WhenAll(tasks); |
Beta Was this translation helpful? Give feedback.
-
I see. Thank you for example! For now, it looks like, the most preferable way. So, furthermore, readability of code could be increased by hiding the creation of the enumerable after a method without additional keywords:
Maybe it would be a little little bit more readable if there was GetAwaiter on the tuple of tasks: |
Beta Was this translation helpful? Give feedback.
-
That's #380 and it already exists as an external library. |
Beta Was this translation helpful? Give feedback.
-
Great! Thank you! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The current async-wait based implementation of asynchronous flows creates some complexity in awaiting of several tasks. For example, to run several operations and to wait until all are done, we should do something like that:
This implementation creates some overhead and decreases the code readability. I saw a lot of code where operations didn’t depend on each other and could be awaited non-sequential, but programmers didn't use the pattern above. I guess it says about the complexity of the pattern, especially for newcomers.
The proposal is to create new keywords, for example, the code above could be replaced with:
or
Another asynchronous blocks, which could be created is sequential await for dependent operations. For example the code:
Could be replaced with:
Also, there could be nested blocks:
Sequential and concurrent methods:
which will become:
Concurrent, sequential blocks/methods could be run only from async methods or other concurrent, sequential.
Beta Was this translation helpful? Give feedback.
All reactions