F# forward pipe to convert from int to bigint

家住魔仙堡 提交于 2019-12-10 13:27:04

问题


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

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

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)

But I really like the Forward Pipe syntax and would like to know if I can achieve it so that...

let IntToBigInt = 10 |> bigint

would work...


回答1:


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


来源:https://stackoverflow.com/questions/3158554/f-forward-pipe-to-convert-from-int-to-bigint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!