可以编译以下代码。

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上收到以下错误?

最佳答案

您可以像这样等待嵌入的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)
}

关于f# - 计算表达式中的`do`块出错了吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50748126/

10-13 09:31