Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I convert a function returning a non-generic Task to ValueTask?

I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync.

In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask, and what should I do if I need to convert a method returning a non-generic Task (that is, the async equivalent of a void method) to ValueTask?

like image 253
Benjamin Hodgson Avatar asked Feb 22 '18 19:02

Benjamin Hodgson


1 Answers

Shot in the dark, but I think it's because Task.CompletedTask is sufficient for most non-generic cases.

One way to think of ValueTask<T> is as a union of Task<T> and T (for asynchronous and synchronous cases respectively). Accordingly a non-generic ValueTask would be a union of Task and... nothing, so just a Task.

I can't think of a case where a non-generic ValueTask would be practically different than caching an already completed Task (which is what Task.CompletedTask is), though I'd love to learn about any.

like image 84
Kevin Montrose Avatar answered Sep 28 '22 16:09

Kevin Montrose