Clearly indicate calls to async functions in synchronous context by introducing a 'sync' keyword #8271
Replies: 2 comments 4 replies
-
I don't see the utility in writing a synchronous method in such a manner. It's not a coroutine, so there's no language support required in order to yield execution. You can already block manually via the methods provided by the task. Manually blocking a Task is also generally considered bad practice as it could lead to deadlocks with synchronization. The language shouldn't make it easier to write such code. |
Beta Was this translation helpful? Give feedback.
-
That's right, but this syntax looks ugly, and does not indicate clearly that the function called is asynchronous, as with
In a non-async context, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The problem
Let's define a function
async object MyFunc()
. In an asynchronous context, we have theawait
keyword to get its result:object x = await MyFunc();
Because of this keyword, it is clearly visible that
MyFunc
is asynchronous, which helps understand the code.Now in a synchronous method, there is no
await
, and a call will look like this:object x = MyFunc().Result;
The problem here is that
.Result
follows exactly the same notation most public class member names follow - Pascal, separated by dots. So this.Result
naturally mixes with all the names to the left of it, thus making it SLOWER to recognize theasync
nature ofMyFunc
. To indicate this nature, the practice is to add theAsync
suffix to a function name. In total, a typical synchronous syntax would be:object x = MyFuncAsync().Result;
The idea
It is suggested to introduce a
sync
keyword so the above call is rewritten as follows:object x = sync MyFunc();
Very similar to:
object x = await MyFunc();
For a function returning non-typed
Task
, thesync
keyword would translate to aWait()
call.Benefits
MyFunc
is asynchronousawait
vssync
, and not yet another Pascal termBeta Was this translation helpful? Give feedback.
All reactions