以下函数中的m1
和m2
存在编译错误。
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
在最后一行被调用。如何忽略它的返回值?以下是唯一的方法吗(编译器会优化它的执行吗?)?
let m1 () =
async {
let! x = m 2
()
}
最佳答案
您可以使用 Async.Ignore
为此:
let m1 () = async { do! m 2 |> Async.Ignore }
来自文档:
Async.Ignore
Creates an asynchronous computation that runs the given computation and ignores its result.
关于f# - 如何忽略异步 block 中异步函数的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762436/