user1206480
user1206480

Reputation: 1858

Is there a way to pattern match against DateTime and Nullable<DateTime>?

I've tried something like this:

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

But this does not work. Is this possible? What is the recommended way of handling a problem like this?

Upvotes: 0

Views: 130

Answers (1)

Petr
Petr

Reputation: 4280

DateTime and Nullable<DateTime> are different types and compiler cannot infer what is the type of parameter entry. Use type annotation:

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

Upvotes: 4

Related Questions