Is the (pseudo) code above executed from an implementation of the expected type (e.g. Task), or maybe this is just how the compiler deals with any expected type that is to the right of the wait keyword?
The pseudo code follows from a specific task. But it exists only to help you understand how it works, because it looks like a sequel.
The actual code generated by the compiler is very different, and it does not take into account the actual type . All he does is look for a GetAwaiter method that returns awaiter with IsCompleted , GetResult , OnCompleted . The expected implementation is one that either captures SynchronizationContext.Current or not (or does something else completely).
You can see the actual code here .
i.e. completely change how the wait works when used with my regular expected object.
You can do whatever you want, if that makes sense to you. You can even create built-in types expected using the extension method. For example, this code:
public static Awaiter GetAwaiter(this string s) { throw new NotImplementedException(); } public abstract class Awaiter : INotifyCompletion { public abstract bool IsCompleted { get; } public abstract void GetResult(); public abstract void OnCompleted(Action continuation); }
Allows you to compile await "bar"; . Of course, it will not work at runtime, but the compiler does not know this.
source share