Implementing a C # Method Returning Task <T> to F #
I am creating a type in F # that inherits from a C # class that provides a method that returns Task<T>in C #. I am trying to decide what would be the best way to do this in F #
Say my C # looks like this:
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
}
My first pass of inheritance of this type in F # looks like this:
type Bar() =
inherits Foo()
override this.DoStuff() =
this.Out.WriteLineAsync("hey from F#") |> ignore
System.Threading.Task.FromResult(SomeEnum.Bar)
But a) it does not feel actually asynchronous, and b) it just does not feel - F #.
So, how could I inherit a class Fooand implement a method DoStuffthat expects to return Task<T>?
+4
3 answers
You can use Async.StartAsTask :
type Bar() =
inherit Foo()
override this.DoStuff() =
async { return SomeEnum.Bar } |> Async.StartAsTask
Async.StartAsTask Async<T> a Task<T>.
+10