您好,所以我目前正在尝试使用事件模式和 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/