Let's say I am solving a particular problem and come up with a function
let function parameter1 ... =
a lot of adding, multiplying & so on with a lot of
literals all over the place
Now this function works fine if my parameters are of type int. But somewhere I will need it to go up to 11, I will need that extra push into int64 or even BigInteger. So what do I do? I copy&paste the function, change the name, and go hunting for all literal appearances that make the compiler think the function should operate on int. And this sucks.
Is there a way to do this:
let greatFunction param1 param2 = (param1+1)/(param2*2)
where param1 and param2 can be any combo of integer types?
Edit:
Expanding a bit on a great tip by kvb below, I came up with the following
module NumericLiteralG
let inline FromZero() = LanguagePrimitives.GenericZero
let inline FromOne() = LanguagePrimitives.GenericOne
let inline FromInt32 n =
let rec loop nIn nOut =
if nIn>0 then loop (nIn - 1) (nOut + LanguagePrimitives.GenericOne)
else nOut
loop n LanguagePrimitives.GenericZero
so it becomes a bit less ugly when used
let inline halfSquare num =
let res = num / 2G
res * res
let solve1 = halfSquare 5I
let solve2 = halfSquare 5.0
let solve3 = halfSquare 5uy
Now the question is should I use this? If not, why not?