首页 > 解决方案 > F#类型注释问题

问题描述

我是 F# 开发的新手。尝试编译示例源代码时出现错误。我有以下代码

let tryParseWith tryParseFunc = tryParseFunc >> function
         | true, v    -> Some v
         | false, _   -> None

let tryParseInt32    = tryParseWith System.Int32.TryParse
let tryParseInt64    = tryParseWith System.Int64.TryParse

但是我遇到了这样的错误

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: Int64.TryParse(s: ReadOnlySpan<char>, result: byref<int64>) : bool, Int64.TryParse(s: string, result: byref<int64>)

标签: .netf#

解决方案


System.Int32(64).TryParse是一个重载的方法。因此,在您的代码中,您必须明确指定要使用的方法的版本。

我想你想解析一个字符串,所以代码应该是:

let tryParseInt32 : string -> int option    = tryParseWith System.Int32.TryParse
let tryParseInt64 : string -> int64 option  = tryParseWith System.Int64.TryParse

推荐阅读