我正在 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>
?
您可以使用异步启动任务 https://msdn.microsoft.com/en-us/library/ee821132.aspx:
type Bar() =
inherit Foo()
override this.DoStuff() =
async { return SomeEnum.Bar } |> Async.StartAsTask
Async.StartAsTask
takes Async<T>
作为输入,并返回Task<T>
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)