`do`块里面的计算表达式得到了错误?

时间:2018-06-07 18:39:17

标签: f#

可以编译以下代码。

async {
    //do (
    use outStream = File.Create(downloading)
    do! httpRequestStreamCopyTo url outStream
    //)
    if File.Exists(fullname) then
        File.Delete(fullname)
    File.Move(downloading, fullname)
}

我需要在重命名前关闭outStream。所以它改为

async {
    do (
        use outStream = File.Create(downloading)
        do! httpRequestStreamCopyTo url outStream // Error
    )
    if File.Exists(fullname) then
        File.Delete(fullname)
    File.Move(downloading, fullname)
}

它在do! httpRequestStreamCopyTo (reportingUri url) outStream上出现以下错误?

  

错误FS0750此构造只能在计算表达式中使用

1 个答案:

答案 0 :(得分:6)

您可以等待嵌入式async正文,以便outStream得到适当的范围:

async {
    do! async {
        use outStream = File.Create(downloading)
        do! httpRequestStreamCopyTo url outStream
    }
    if File.Exists(fullname) then
        File.Delete(fullname)
    File.Move(downloading, fullname)
}

由于嵌入式主体是阻塞的,因此这在概念上等同于顺序async调用:

async {
    use outStream = File.Create(downloading)
    do! httpRequestStreamCopyTo url outStream
}
async {
    if File.Exists(fullname) then
        File.Delete(fullname)
    File.Move(downloading, fullname)
}