Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Task.FromResult(0) from FSharp

Tags:

f#

I am rewriting some legacy C# classes into F#. One of the classes implements the IIdentityMessageService with the single method of this.SendAsync(identityMessage)

In the C# code, I see this

    if (transportWeb != null)
        return transportWeb.DeliverAsync(message);
    else
        return Task.FromResult(0);

I tried it in F# like this

    if  transportWeb != null then
        transportWeb.DeliverAsync(message)
    else
        Task.FromResult(0)

and I am getting an error on the last line

This expression was expected to have type
    Task    
but here has type
    Task<'a>    

What am I missing? Thanks

like image 852
Jamie Dixon Avatar asked Feb 11 '23 13:02

Jamie Dixon


1 Answers

It looks like DeliverAsync returns a Task, so you need both branches to return a Task. Task.FromResult(0) returns a Task<int> so you need to explictly upcast it to a Task:

else
    Task.FromResult(0) :> Task
like image 56
Lee Avatar answered Feb 16 '23 01:02

Lee