2016-07-13 33 views
3

我從here解決方案使用DateTime.TryParseExact(這也是讓比DateTime.TryParse友好少得多)成一個DateTime選項F#活躍模式沒有被定義

爲了節省您點擊下面,這裏的驗證碼:

let (|DateTimeExact|_|) (format: string) s = 
    match DateTime.TryParseExact(s, format, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None) with 
    | true, d -> Some d 
    | _ -> None 

當我嘗試使用它(在同一模塊中我也試過,沒有運氣同樣的功能中定義它),

match DateTimeExact "M-d-yyyy" dateStr with 
| _ ->() 

的Visual Studioü nderlines「DateTimeExact」出現錯誤:

'The value or constructor 'DateTimeExact' is not defined

我在做什麼錯?當我將鼠標懸停在活動模式的let綁定上時,我看到

val(| DateTimeExact|_|) : (string -> string -> DateTime option) 

回答

6

您的語法根本不正確; correct syntax是:

// 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" 
+0

謝謝你們的幫助! –