4

Binding tasks together with https://github.com/louthy/language-ext requires tasks with return type (Task<>). Tasks without return type should therefore be converted into Task<Unit>.

Does anybody know a compact (expression only) way to convert Task to Task<Unit> in C# -- with (or without) using Language-Ext?

In other words: Is there something like fun(...) for Task?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
stb
  • 772
  • 5
  • 15
  • 1
    Why do you need a (compact, expression only) way to do that using Language-Ext? Can't you do that (compact, expression only) using plain C#? – superjos Sep 03 '18 at 15:18
  • 1
    Oh, you're right. I implied that there is no compact way in plain C#. If you know any, please add it here. I will modify the question to make this clear for everybody. – stb Sep 03 '18 at 18:00
  • 1
    well, of course the library author has already given an exhaustive answer :) – superjos Sep 03 '18 at 20:27

1 Answers1

4

I'm not able to test this right now, but it should do what you want.

public static class TaskExtensions
{
    public static async Task<Unit> ToUnit(this Task task)
    {
        await task;
        return unit;
    }
}

Then call:

task.ToUnit();

On your untyped tasks. I'll probably add this to lang-ext at some point.

louthster
  • 1,560
  • 9
  • 20