【问题标题】:F# active pattern is not definedF# 活动模式未定义
【发布时间】:2016-07-13 19:38:38
【问题描述】:

我正在遵循 here 的解决方案,将 DateTime.TryParseExact(它比 DateTime.TryParse 不太友好)用于 DateTime 选项

为了节省您的点击,这里是代码:

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 在“DateTimeExact”下划线并显示错误:

'未定义值或构造函数'DateTimeExact'

我做错了什么?当我将鼠标悬停在活动模式的 let 绑定上时,我看到了

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

【问题讨论】:

    标签: f#


    【解决方案1】:

    您的语法完全不正确; 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"
    

    【讨论】:

    • 感谢两位的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多