gpt4 book ai didi

c# - 在 F# 中实现返回 Task 的 C# 方法

转载 作者:太空狗 更新时间:2023-10-30 00:14:16 25 4
gpt4 key购买 nike

我在 F# 中创建一个继承自 C# 类的类型,该类公开返回 Task<T> 的方法在 C# 中。我正在尝试找出在 F# 中执行此操作的最佳方法

假设我的 C# 看起来像这样:

public class Foo {
public TextWriter Out { get { return Console.Out; } }

public virtual Task<SomeEnum> DoStuff() {
return Task.FromResult(SomeEnum.Foo);
}
}

public enum SomeEnum {
Foo,
Bar
}

我在 F# 中继承该类型的第一步看起来是这样的:

type Bar() =
inherits Foo()

override this.DoStuff() =
this.Out.WriteLineAsync("hey from F#") |> ignore

System.Threading.Task.FromResult(SomeEnum.Bar)

但是 a) 它并不像实际上是异步的 b) 它只是感觉不是 F#。

那么我将如何继承 Foo类并实现 DoStuff期望返回 Task<T> 的方法?

最佳答案

您可以使用 Async.StartAsTask :

type Bar() =
inherit Foo()
override this.DoStuff() =
async { return SomeEnum.Bar } |> Async.StartAsTask

Async.StartAsTask需要 Async<T>作为输入,并返回 Task<T> .

关于c# - 在 F# 中实现返回 Task<T> 的 C# 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30793392/

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