问题
How to guard with DateTime.TryParseExact
(and get the parsed value if possible)? The following code doesn't work.
[<EntryPoint>]
let main args =
let argList = args |> List.ofSeq
match argList with
| "aaa" :: [] -> aaa.main "aaa"
| "bbb" :: [] -> bbb.main "bbb"
| "ccc" :: yyyymm :: [] when DateTime.TryParseExact
(yyyymm, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None)->
ccc.main "ccc" yyyymm
回答1:
You can use a mutable
:
let mutable dt = Unchecked.defaultof<_>
match argList with
| "ccc" :: yyyymm :: [] when
DateTime.TryParseExact(yyyymm,
"yyyyMM",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
&dt) -> ...
But an active pattern makes the match much clearer:
let (|DateTimeExact|_|) (format: string) s =
match DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture, DateTimeStyles.None) with
| true, d -> Some d
| _ -> None
match argList with
| "ccc" :: DateTimeExact "yyyyMM" yyyymm :: [] -> ...
来源:https://stackoverflow.com/questions/26809483/pattern-match-guard-with-datetime-tryparseexact