Fun with generics...

Fun with generics...

Well, in Delphi it's a bit hard. I'm used to C++ templates, which allow for much more flexibility. I'm trying to get back into using generics instead of templates, and I could use a bit of help getting out of the box I feel generics trap me in...

Here's my attempt at writing a generic Kahan summation algorithm (those of you who read non-tech might recognize it).

I've included a toy fixed point type, to make sure it works with custom number types.

Now, what I don't like is that due to the Delphi compiler's inept parameter deduction, I have to write
mySum := TAlgorithms.Sum(myArray);

The declaration of Sum is
function Sum>(const A: TArray): T;

Now, here's where I'm rusty... Is there any way to change this code so that I only have to specify one of the parameters, for example
mySum := TAlgorithms.Sum(myArray);

At least then I wouldn't have to worry about matching two parameters all the time.
http://nopaste.dk/p60306

Comments