F#转发管道以将int转换为bigint [英] F# forward pipe to convert from int to bigint
问题描述
我对F#相当陌生,并且遇到了这种情况,希望有人能解释为什么我的编译器不喜欢该代码...
I am fairly new to F# and came up across this scenario and was hoping someone could explain why my compiler doesnt like the code...
如果在F#中执行以下操作...
If in F# I do the following...
let FloatToInt = 10.0 |> int
let IntToFloat = 10 |> float
一切都很好,并将数字强制转换为相关的数据类型...
Everything is fine and the number is cast to the relevant data type...
但是,如果我执行以下操作...
if however I do the following...
let IntToBigInt = 10 |> bigint
我收到一个错误类型名称或对象构造函数的无效使用". 我认为这是因为bigint的正向管道没有运算符重载吗?
I get a error "Invalid use of type name or object constructor." I assume that this is because there isnt an operator overload for the forward pipe for bigint?
如果我想使此代码成为可能,该怎么办?我知道我可以使用其他语法,例如...
If I wanted to make this code possible, how would I do it? I know I could use different syntax like...
let IntToBigInt = bigint(10)
但是我真的很喜欢Forward Pipe语法,想知道我是否可以实现它,以便...
But I really like the Forward Pipe syntax and would like to know if I can achieve it so that...
let IntToBigInt = 10 |> bigint
会工作...
推荐答案
它与重载无关. 10.0 |> int
之所以有效,是因为有一个名为int
的函数.但是,没有名为bigint
的函数,因此10 |> bigint
不起作用.
It has nothing to do with overloads. 10.0 |> int
works because there is a function named int
. There is however no function named bigint
, so 10 |> bigint
does not work.
如果您定义一个,它将起作用:
If you define one, it works:
> let bigint (x:int) = bigint(x);; // looks recursive, but isn't
val bigint : int -> System.Numerics.BigInteger
> 42 |> bigint;;
val it : System.Numerics.BigInteger = 42I
这篇关于F#转发管道以将int转换为bigint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!