Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# active pattern is not defined

Tags:

f#

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)
like image 800
reborn programmer Avatar asked Jun 02 '26 04:06

reborn programmer


1 Answers

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"
like image 106
ildjarn Avatar answered Jun 04 '26 13:06

ildjarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!