Can async-await be available in other .NET languag

2019-06-21 21:01发布

问题:

The async-await pattern is one of the more powerful additions to .NET 4.5 and C# 5.0

It is currently only available in C# and VB as far as I know.

Is this API a feature of the .NET CLR or of the C# compiler itself?

What I'm really asking is this - should we expect async/await in the other .NET languages such as:

  • C++ (via C++/CLI)
  • Python (via IronPython)
  • PHP (via Phalanger)
  • JavaScript (via IronJS)

Please note I'm not asking about asynchronous programming in general, but about the specifics of the async-await pattern (asynchronous code which is written synchronously).

回答1:

Is this API a feature of the .NET CLR or of the C# compiler itself?

This is a feature of the compiler. There were no CLR changes required to support async and await. Other languages would need to be extended to support it. There is nothing preventing a compiler from doing this, but it would have to be changed in the compiler and language itself.

C++ does support some asynchrony in Windows Store applications via task/then, though it's not as usable as the async/await keywords in C# and VB.

It is currently only available in C# and VB as far as I know

Note that F# also has async support (which predates the C# and VB.Net versions by quite a bit). It is different than the C# style async support, but similar (and inspired the C# and VB.Net teams).



回答2:

The C# and VB.Net compilers implement the await pattern by transforming the original method into a state machine. This is very similar to say how the F# async / work flow pattern works. This type of transformation is possible in any language that targets the CLR.



回答3:

It is a feature of the compiler that requires BCL support.

For example, there is a NuGet package that allows you to use it from .NET 4.0 by adding the various APIs (e.g. Task.GetAwaiter) that the language support leverages.

It's conceptually similar to yield return, which is another compiler feature that any language could conceivably implement.



回答4:

You can look into the Visual C++ Compiler November 2013 CTP, some nifty examples to do things like: __await FileIO::WriteTextAsync(file, file->Name);

It doesn't look as nice yet, and it is likely the standard evolving, new and better (at least standard) ways to do this will emerge in future versions.