gpt4 book ai didi

f# - C# async/await 方法到 F#?

转载 作者:行者123 更新时间:2023-12-04 01:50:51 25 4
gpt4 key购买 nike

我正在尝试学习 F#,并且正在将一些 C# 代码转换为 F#。

我有以下 C# 方法:

public async Task<Foo> GetFooAsync(byte[] content)
{
using (var stream = new MemoryStream(content))
{
return await bar.GetFooAsync(stream);
}
}

在哪里 bar是一些私有(private)领域和 GetFooAsync返回 Task<Foo> .

这如何转化为 F#?

这是我目前拥有的:
member public this.GetFooAsync (content : byte[]) = 
use stream = new MemoryStream(content)
this.bar.GetFooAsync(stream)

返回 Task .

最佳答案

在 F# 中,异步由 async 表示。计算生成器,它不是 Task 的精确模拟, 但通常可以用来代替一个:

member public this.GetFooAsync (content : byte[]) = 
async {
use stream = new MemoryStream(content)
return! this.bar.GetFooAsync(stream) |> Async.AwaitTask
}
|> Async.StartAsTask

关于f# - C# async/await 方法到 F#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com