Jamie Dixon
Jamie Dixon

Reputation: 4292

Using Task.FromResult(0) from FSharp

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

Upvotes: 4

Views: 521

Answers (1)

Lee
Lee

Reputation: 144136

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

Upvotes: 9

Related Questions