【发布时间】:2017-02-09 00:28:08
【问题描述】:
我刚开始使用 F#,我发现下面显示的行为令人费解。有什么解释吗?
let bg = bigint 1;;
(* val bg : System.Numerics.BigInteger = 1 *)
let bg = (bigint) 1;;
(* error FS0041: A unique overload for method 'BigInteger' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: System.Numerics.BigInteger(value: byte []) : unit, System.Numerics.BigInteger(value: decimal) : unit, System.Numerics.BigInteger(value: float) : unit, System.Numerics.BigInteger(value: float32) : unit, System.Numerics.BigInteger(value: int) : unit, System.Numerics.BigInteger(value: int64) : unit, System.Numerics.BigInteger(value: uint32) : unit, System.Numerics.BigInteger(value: uint64) : unit *)
【问题讨论】:
-
你为什么希望第二个工作?
-
因为与第一个的唯一区别是括号。例如,
let j = int "4";;和let k = (int) "4";;都工作并且具有相同的输出val j : int = 4。 -
@Soldalma: 'int' 实际上是 Core 库中定义的一个函数,它使用一些神秘的内联来找到正确的类型转换,因此它在使用括号时也有效。另请参阅github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/… 第 4262 行。
标签: f#