本文介绍了实现C#方法返回任务< T>在F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的F#类型从该公开返回任务℃的方法的C#类继承; T> 在C#。我试图找出是什么了这样做,在F#中的最佳方式。

I'm creating a type in F# that inherits from a C# class that exposes a method that returns Task<T> in C#. I'm trying to work out what'd be the best way to do that in 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#中该类型的第一遍看起来像这样:

My first pass of inheriting that type in F# looks like so:

type Bar() =
    inherits Foo()

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

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

但)它不觉得它实际上异步和b)只是感觉不-F#。

But a) it doesn't feel like it's actually async and b) it just feels not-F#.

所以,我怎么会去继承类,并实现了预计返回的 DoStuff 方法任务&LT; T&GT;

So how would I go about inheriting the Foo class and implement the DoStuff method that expects to return a Task<T>?

推荐答案

您可以使用

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

Async.StartAsTask 需要异步&LT; T&GT; 作为输入,并返回一个任务&LT ; T&GT;

Async.StartAsTask takes Async<T> as input, and returns a Task<T>.

这篇关于实现C#方法返回任务&LT; T&GT;在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 00:05