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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With