F# - 将任务<unit>转换为plain Task

时间:2018-01-31 23:21:19

标签: .net f# task-parallel-library

我使用Giraffe使用Async工作流创建本机.NET任务,因为我需要与Discord.NET库的互操作性。我有一个Task的事件,但我写的事件处理程序返回Task<unit>但由于某种原因,类型检查器不够智能意识到它是一回事。有没有办法将Task<unit>转换为直线Task

这是我的代码:

let messageReceived (msg : SocketMessage) =
    task {
        let author = msg.Author.Username
        let text = msg.Content
        match msg.Author.Username with
            | "Monika" -> ()
            | usr ->
                if text.ToLower().StartsWith "<@407367655830585344> say" then
                    text.Substring 26 |> msg.Channel.SendMessageAsync |> ignore
                else if text.ToLower().StartsWith "<@407367655830585344>" then
                    let response = (Lines.VoiceLines |> Extras.randomElement).Replace("[player]", author)
                    msg.Channel.SendMessageAsync response |> ignore
                else if text.ToLower().StartsWith "delete" then
                    text.Substring 7 + ".chr deleted" |> msg.Channel.SendMessageAsync |> ignore
                else if Extras.onein 10 then
                    let response = (Lines.VoiceLines |> Extras.randomElement).Replace("[player]", author)
                    msg.Channel.SendMessageAsync response |> ignore
    }

_client.add_MessageReceived (fun m -> messageReceived m)

最后一行生成错误,说MessageReceived事件需要一个带有签名SocketMessage -> Task的函数:

  

错误FS0001:此表达式应该具有类型&#39;任务&#39;但这里有类型&#39;任务&lt; unit&gt;&#39;

1 个答案:

答案 0 :(得分:10)

TaskTask<T>的基类,因此您应该能够使用Task<unit>运算符将Task转发为:>。尝试这样的事情:

(fun m -> messageReceived m :> Task)

我认为这个最小的例子再现了这个问题:

// int -> Task<unit>
let sleepTaskT (m : int) = Async.Sleep m |> Async.StartAsTask
// (int -> Task) -> Task
let foo (f : int -> Task) = f 1000
// "This expression was expected to have type 'Task' but here has type 'Task<unit>'"
foo (fun m -> sleepTaskT m)
// this works by explicitly upcasting sleepTaskT's Task<unit> to Task
foo (fun m -> sleepTaskT m :> Task)