将F#函数a b-> c转换为C#Func <a,b,=“,-=”“> c <bool >>

时间:2018-07-07 20:44:10

标签: c# f# embedio

在C#中:

    private Task<bool> HandleRequest(HttpListenerContext context, CancellationToken ct)

成为F#:

let HandleRequest (context:HttpListenerContext, ct:CancellationToken) =
    Task.FromResult(false)
//or
let HandleRequest (context:HttpListenerContext) (ct:CancellationToken) =
    Task.FromResult(false)

现在我需要调用

let toFunc<'I, 'T> f =
        System.Func<'I,'T> f

type FunApi() as this =
  inherit WebModuleBase()

  do
      let handle = HandleRequest |> toFunc
      this.AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, handle)

但是我得到了错误:

/..../Data.fs(57,57): Error FS0001: This expression was expected to have type    
'Func<HttpListenerContext,CancellationToken,Task<bool>>'    but here has type    
'Func<(HttpListenerContext * CancellationToken),Task<bool>>' (FS0001)

1 个答案:

答案 0 :(得分:3)

Func需要三个参数,因为它具有HttpListenerContext和CancellationToken作为参数,并返回Task。

open System.Threading.Tasks
open System.Net

//or
let HandleRequest (context:HttpListenerContext) (ct:CancellationToken) =
    printfn "%s" "Hello World"
    Task.FromResult(false)

let toFunc<'a, 'b, 'c> f =
        System.Func<'a, 'b, 'c> f

type FunApi() as this =
  inherit WebModuleBase()

  do
      let handle = HandleRequest |> toFunc
      this.AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, handle)

  override __.Name = "BlaBla"


[<EntryPoint>]
let main args =
    use server = new WebServer("http://localhost:9696/")
    server.RegisterModule(new FunApi())
    server.RunAsync() |> ignore
    Console.ReadLine() |> ignore
    0