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?
I think Generic Arithmetic is common problem in .NET languages.
There are many articles explaining different approaches and very soon I will post another one explaining mine which is similar to the solution you posted.
Now, if you ask me if should you use it, I would say: as long as you understand what you are doing why not? I’m using it partially in production and have no issues at all, but because I care about run-time performance I use overloading to resolve everything at compile time.
Then to speed up compile time I redefine basic math operators to operate in the same type, otherwise type signatures get really complicated and may take ages to compile.
There are more things to consider but for your specific problem here is a sample code: