Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# forward pipe to convert from int to bigint

Tags:

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

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

like image 397
Mark Pearl Avatar asked Jul 01 '10 14:07

Mark Pearl


1 Answers

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
like image 96
sepp2k Avatar answered Oct 18 '22 08:10

sepp2k