I'm following the solution from here for using the DateTime.TryParseExact (which is so much less friendly than DateTime.TryParse) into a DateTime option
To save you a click, here's the code:
let (|DateTimeExact|_|) (format: string) s =
match DateTime.TryParseExact(s, format, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None) with
| true, d -> Some d
| _ -> None
When I try to use it (within the same module. I've also tried defining it within the same function with no luck),
match DateTimeExact "M-d-yyyy" dateStr with
| _ -> ()
Visual Studio underlines "DateTimeExact" with an error:
'The value or constructor 'DateTimeExact' is not defined
What am I doing wrong? When I hover over the let binding for the active pattern, I see
val( | DateTimeExact|_|) : (string -> string -> DateTime option)
Your syntax is simply incorrect; the correct syntax is:
// incomplete pattern, but if you _know_, you know
match dateStr with DateTimeExact "M-d-yyyy" _ -> ()
// complete pattern
match dateStr with
| DateTimeExact "M-d-yyyy" _ -> ()
| _ -> failwith "didn't match"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With