[Issue 12169] sum(int[]) should return a int

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Feb 15 06:32:34 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12169



--- Comment #3 from bearophile_hugs at eml.cc 2014-02-15 06:32:29 PST ---
(In reply to comment #1)
> This is by design in the code, presumably to avoid overflow (which you are
> usually so concerned about, what changed?)

I have tens of usages of sum in my code and no case I need a long result. So
the Phobos sum halves my usability. And I need to keep my own sum() around.


> I actually agree with you though. I'd expect sum!(int[]) to return int. In
> general, I'd expect sum!(T[]) to return typeof(T.init + T.init), and respect
> the language integral promotions.

Right.


> A nice compromise might be to adjust the API:
> 
> int[] xs;
> int sum1 = sum(xs);
> long sum2 = sum!long(xs);
> 
> That way, you can specify what type to return. This also solves the problem of
> potential overflows with longs (just specify ReturnType = BigInt).

This is OK, but an alternative (and in my opinion better) solution is in Issue
12173:

long sum2 = xs.sum(0L);

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list