【问题标题】:Is there a way to pattern match against DateTime and Nullable<DateTime>?有没有办法对 DateTime 和 Nullable<DateTime> 进行模式匹配?
【发布时间】:2015-09-02 00:23:14
【问题描述】:

我尝试过这样的事情:

type DateResult = | ValidDate of DateTime | NullableDate | BlahBlah

let validateDate entry =
    match entry with
    | :? DateTime as x -> ValidDate x // error here
    | :? Nullable<DateTime> as x -> NullableDate
    | _ BlahBlah

但这不起作用。这可能吗?处理此类问题的推荐方法是什么?

【问题讨论】:

    标签: f#


    【解决方案1】:

    DateTimeNullable&lt;DateTime&gt; 是不同的类型,编译器无法推断参数entry 的类型。使用类型注解:

    let validateDate (entry: obj) =
        match entry with
        | :? DateTime as x -> ValidDate x 
        | :? Nullable<DateTime> as x -> NullableDate
        | _ BlahBlah
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多