f# - TryParse 字符串到 Int 事件模式

标签 f#

您好,所以我目前正在尝试使用事件模式和 Int.TryParse 方法将字符串解析为 Int。使用 2012 年的旧示例,它曾经是这样工作的:

let (|Int|_|) str =
    match System.Int32.TryParse str with
    | true,int -> Some int
    | _ -> None

但现在我收到一个错误,告诉我无法选择 TryParse() 的正确重载。将 str 注释为字符串,错误仍然存​​在。我希望你们中的任何人都可以帮助我解决这个简单的问题,在此先感谢。

一些附加信息:我正在尝试使用 FSharp.Core 4.5.2 和 .Net Core 2.1。

错误:

FS0041 A unique overload for method 'TryParse' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: Int32.TryParse(s: ReadOnlySpan<char>, result: byref<int>) : bool, Int32.TryParse(s: string, result: byref<int>) : bool

最佳答案

Jestar342 的帮助下发现了我自己的错误在 Reddit 上:

let (|Int|_|) (str:string) =
    match System.Int32.TryParse str with
    | true,int -> Some int
    | _ -> None

将 str 更改为 (str:string) 可修复此错误。

关于f# - TryParse 字符串到 Int 事件模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52978362/

相关文章:

functional-programming - 正确理解F#预计算逻辑

visual-studio - 为什么我不能让 Suave 在 VS 2017 上工作?

f# - 类型 x 比值更难访问

f# - 在 F# 中自动打开命名空间

ide - 是否有任何适用于 Mono 的 F# IDE?

linq - F# groupBy - 系统异常 : unrecognized method call

F# 有区别的联合类型问题

websocket - 如何在suave中通过websocket实现服务器推送?

f# - F# 中的签名文件和访问修饰符

asynchronous - AsyncSeq 未在交互式中打印到控制台