await out as a shorthand for TaskCompletionSource #2664
Replies: 5 comments
-
public static class TaskHelpers {
public static Action<T> AwaitCallback<T>(out Task<T> task) {
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
task = tcs.Task;
return value => tcs.TrySetResult(value);
}
} LegacyCallback(AwaitCallback(out var task));
var result = await task; |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Could your |
Beta Was this translation helpful? Give feedback.
-
I don't believe generic inference can handle that today, no. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Right, that's why I request this as language feature so it would just guess the type and transpile by compiler |
Beta Was this translation helpful? Give feedback.
-
There are other proposals that seek to improve generic type inference in these kinds scenarios. I'd suggest that would probably be a better approach. This language feature seems like it would only benefit a very few narrow cases and only if the compiler always assumes that a function that accepts a delegate represents some kind of async callback, which is very rarely the case. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There was so many legacy API that utilize callback for
async
. Even if we could use TaskCompletionSource it almost always verbose. I think we should have some syntax to shorthand the code that would always the sameMaybe
await out
like thisIf the callback is
Action<T0,T1>
then it would convert to tupleBeta Was this translation helpful? Give feedback.
All reactions