Abdul Manaf
Abdul Manaf

Reputation: 4993

F# Type annotation issue

I am new in F# development. I got error when trying to compile the sample source code. I have following code

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

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

But I got an error like this

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>)

Upvotes: 1

Views: 99

Answers (1)

Nghia Bui
Nghia Bui

Reputation: 3784

System.Int32(64).TryParse is an overloaded method. Thus in your code you have to explicitly specify the version of the method you want to use.

I think you want to parse a string, so the code should be:

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

Upvotes: 2

Related Questions