A few related questions about the async CTP:
-
I can iterate over an Iterator Block (an
IEnumerable<T>yield-returningT) usingGetEnumerator()and then enumerator methodsMoveNext(), andCurrent(). What is the analog forasyncmethods? How can a non-asynccalling method to receive and process anyawaited items and thenContinueWith()? Can you provide a short example? I’m just not seeing it. -
Also, in this following example
asyncmethod,MyAwaitablehas aGetAwaiter()method. IfGetAwaiter()returns astringbutTHuhis notstring, the compiler doesn’t complain. What type constraints/expectations exist betweenTHuhandGetAwaiter()?async Task<THuh> DoSomething() { var x = await new MyAwaitable("Foo"); var y = await new MyAwaitable("Bar"); return null; } -
Please explain the following line of the C# spec draft. Are
async Task<T>methods supposed toreturnadefault(T)which will never be used? I see some samples that do not seem to follow this rule – the return value seems reachable and the value is non-default. Is this value inaccessible? If so, why the awkward inaccessible return statement?
In an asynchronous function with the return type
Task<T>for someT,
return statements must have an expression that is implicitly
convertible toT, and the endpoint of the body must be unreachable.
- The spec says “All of GetAwaiter, IsCompleted, OnCompleted and GetResult are intended to be “non-blocking”” – so then in what method should the (potentially) long-running operation be defined?
Thanks!
I can understand how you might feel that way. I discourage people from trying to build CPS out of iterator blocks because really it is not a good fit, no matter what underlying mechanisms iterators and CPS have in common. Iterator blocks are designed to feel good for quickly making methods that turn data structures into sequences or turn sequences into different sequences; they’re not designed to solve the general problem of call-with-current-continuation.
For that matter, async/await isn’t precisely call-with-current-continuation either, though it comes an order of magnitude closer, obviously. Async/await is designed to make task-based asynchrony easier; that it does so by rewriting code into a form of continuation passing style is an implementation detail.
This answer I wrote on a related topic might help:
How could the new async feature in c# 5.0 be implemented with call/cc?
I suspect that the conceptual problem that you’re having is that in iterator-style asynchrony, the “orchestrator” — the thing figuring out when the iterator block gets to resume where it left off — is your code. You write some code and you decide when to call MoveNext to pump the iterator. With task-based asynchrony, some other hunk of code does that for you. When a task completes, odds are good that it posts that fact to a message queue somewhere, and then when the message queue is pumped, the continuation gets activated with the result. There’s no explicit “MoveNext” in your code that you can point at; rather, the fact that a task has completed and knows its own continuation is sufficient to ensure that the continuation is put onto a work queue for eventual execution.
If you’ve got more questions, I encourage you to post them on SO and/or the async forum.