Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 12434 - std.algorithm.sum of immutable array too #2071

Merged
1 commit merged into from
Apr 7, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions std/algorithm.d
Original file line number Diff line number Diff line change
Expand Up @@ -1090,10 +1090,10 @@ if (isInputRange!R && !isInfinite!R && is(typeof(r.front + r.front)))
{
alias E = Unqual!(ElementType!R);
static if (isFloatingPoint!E)
typeof(E.init + 0.0) seed = 0; //biggest of double/real
alias Seed = typeof(E.init + 0.0); //biggest of double/real
else
typeof(r.front + r.front) seed = 0;
return sum(r, seed);
alias Seed = typeof(r.front + r.front);
return sum(r, Unqual!Seed(0));
}
/// ditto
auto sum(R, E)(R r, E seed)
Expand Down Expand Up @@ -1216,6 +1216,24 @@ unittest
assert(sum(SList!double(1, 2, 3, 4)[]) == 10);
}

unittest // 12434
{
immutable a = [10, 20];
auto s1 = sum(a); // Error
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of context the comments will look weird, I suggest adding // 12434.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry. Yeah, I usually reference the bug number when doing such copy pastes. I guess I forgot this time. Fixed.

auto s2 = a.map!(x => x).sum; // Error
}

unittest
{
import std.bigint;
immutable BigInt[] a = BigInt("1_000_000_000_000_000_000").repeat(10).array();
immutable ulong[] b = (ulong.max/2).repeat(10).array();
auto sa = a.sum();
auto sb = b.sum(BigInt(0)); //reduce ulongs into bigint
assert(sa == BigInt("10_000_000_000_000_000_000"));
assert(sb == (BigInt(ulong.max/2) * 10));
}

/**
Fills $(D range) with a $(D filler).
*/
Expand Down