I don't use delphi generics often but I was curious if the following line of C# can be done in the same way as in C# using the generics units included on Delphi XE :

I don't use delphi generics often but I was curious if the following line of C# can be done in the same way as in C# using the generics units included on Delphi XE :

double payments = _payments.Sum(p => p.Calculate(rate));

_payments is a generic list on .NET defined more or less like this: List and of course MyClass has a method: Calculate( double percent )

What do you think ?

Comments

  1. I think spring4d has a method on IEnumerable or in the helper record Enumerable on the 1.2 branch that allows you to do that

    ReplyDelete
  2. Spring4D IEnumerable (and thus IList aswell) has a Sum method but no overload where you can pass a selector function (if you look at the code you can see I once put them there but they are commented out because every method on the interface causes binary growth for every IEnumerable specialization): As much as I hate saying that - given that implementing such a function in a generic way in Delphi causes quite some overhead I think it would be the easiest and most pragmatic to use a for loop:

    payments := 0;
    for p in _payments do
    payments := payments := p.Calculate(rate);

    ReplyDelete
  3. Stefan Glienke I think I'll go that way, there is a small set of classes I'm translating from C# and this line was the one that will require "more effort" but I think it would be an overkill to include Spring4d just for this and after what you said it is even clearer for me what should be done, thank you.

    ReplyDelete

Post a Comment