2016-11-30 106 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

完美的作品,谢谢您。并感谢您的链接。 –