2016-11-30 83 views
3

我寫一個使用Quartz.Net以輪詢其他服務(已經用C#編寫)的F#服務:與DateTime.MinValue匹配的模式?

[<PersistJobDataAfterExecution>] 
[<DisallowConcurrentExecution>] 
type PollingJob() = 
    interface IJob with 
     member x.Execute(context: IJobExecutionContext) = 
      let uri = "http://localhost:8089/" 
      let storedDate = context.JobDetail.JobDataMap.GetDateTime("LastPoll") 
      //this works... 
      let effectiveFrom = (if storedDate = DateTime.MinValue then System.Nullable() else System.Nullable(storedDate)) 

      let result = someFunction uri effectiveFrom 

      context.JobDetail.JobDataMap.Put("LastPoll", DateTime.UtcNow) |> ignore 

context傳遞給Execute功能由石英爲每個投票和包含字典。服務第一次運行時,LastPoll的值將爲默認的DateTime值,即01/01/0001 00:00:00。然後下一次調度程序運行LastPoll將包含上次輪詢的時間。

我可以使用if..then..else結構(上圖)創建Nullable<DateTime>但是當我嘗試使用模式匹配我得到一個編譯錯誤與波浪線下DateTime.MinValue

這個字段是不是文字,不能在圖案中使用

我想使用的代碼如下:

//this doesn't... 
let effectiveFrom = 
    match storedDate with 
    | DateTime.MinValue -> System.Nullable() 
    | _ -> System.Nullable(storedDate) 

回答

3

您正在使用模式匹配稍有不正確。

下應該工作:

let effectiveFrom = 
     match storedDate with 
     | d when d = DateTime.MinValue -> System.Nullable() 
     | _       -> System.Nullable(storedDate) 

當你想測試平等的模式匹配的一部分,你需要使用時,條款(見這裏 - https://fsharpforfunandprofit.com/posts/match-expression/

+2

完美的作品,謝謝您。並感謝您的鏈接。 –