2

The m1 and m2 in the following functions have compiling errors.

let m p = async { return p * 2 }
let m1 () = async { do! m 2 } // ERR: was expected 'int' but here has type 'unit'
let m2 () = async { do! m 2 |> ignore } // ERR: expecting 'Async<int>->Async<'a>' but given 'Async<int>->unit'

m is called at the last line. How to ignore its return value? Is the following the only way (will executing of it be optimized by the compiler?)?

let m1 () = 
    async { 
      let! x = m 2 
      () 
    }
1
  • Are you sure you want to do this? It smells C# Commented Jun 8, 2018 at 17:20

1 Answer 1

6

You can use Async.Ignore for this:

let m1 () = async { do! m 2 |> Async.Ignore }

From the documentation:

Async.Ignore Creates an asynchronous computation that runs the given computation and ignores its result.

Sign up to request clarification or add additional context in comments.

2 Comments

btw, it is literally the same as let m1 () = m 2 |> Async.Ignore
The signature of m1 (the return type) are different?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.