## digitalmars.D.learn - Why does sum not work in static arrays?

• Tim K. (26/26) Dec 06 2015 Hi! I have the following code:
• drug (3/29) Dec 06 2015 Because static array aren't ranges, but dynamic ones are. Try this:
• cym13 (7/35) Dec 06 2015 So that you do not shoot yourself in the foot too easily. A
• Tim K. (7/11) Dec 06 2015 Oh, right, I totally forgot about that. Thank you for reminding
Tim K. <invalid webinterface.com> writes:
```Hi! I have the following code:

int main(string[] argv)
{
import std.algorithm: sum;
import std.stdio: writeln;

uint[3] a1 = [1, 2, 3];
uint[] a2;
for (int i = 1; i <= 3; ++i)
a2 ~= i;

writeln("a1: ", sum(a1));
writeln("a2: ", sum(a2));
return 0;
}

This throws the error:
dummy.d(11): Error: template std.algorithm.iteration.sum cannot
deduce function from argument types !()(uint[3]), candidates are:
/usr/include/dmd/phobos/std/algorithm/iteration.d(3916):
std.algorithm.iteration.sum(R)(R r) if (isInputRange!R &&
!isInfinite!R && is(typeof(r.front + r.front)))
/usr/include/dmd/phobos/std/algorithm/iteration.d(3927):
std.algorithm.iteration.sum(R, E)(R r, E seed) if (isInputRange!R
&& !isInfinite!R && is(typeof(seed = seed + r.front)))

So a dynamic array works just fine. In fact, if I uncomment the
line in question the program compiles and outputs the correct
value (for a2). Why does "sum" not work on static arrays?

Regards
```
Dec 06 2015
drug <drug2004 bk.ru> writes:
```06.12.2015 15:23, Tim K. пишет:
Hi! I have the following code:

int main(string[] argv)
{
import std.algorithm: sum;
import std.stdio: writeln;

uint[3] a1 = [1, 2, 3];
uint[] a2;
for (int i = 1; i <= 3; ++i)
a2 ~= i;

writeln("a1: ", sum(a1));
writeln("a2: ", sum(a2));
return 0;
}

This throws the error:
dummy.d(11): Error: template std.algorithm.iteration.sum cannot deduce
function from argument types !()(uint[3]), candidates are:
/usr/include/dmd/phobos/std/algorithm/iteration.d(3916):
std.algorithm.iteration.sum(R)(R r) if (isInputRange!R && !isInfinite!R
&& is(typeof(r.front + r.front)))
/usr/include/dmd/phobos/std/algorithm/iteration.d(3927):
std.algorithm.iteration.sum(R, E)(R r, E seed) if (isInputRange!R &&
!isInfinite!R && is(typeof(seed = seed + r.front)))

So a dynamic array works just fine. In fact, if I uncomment the line in
question the program compiles and outputs the correct value (for a2).
Why does "sum" not work on static arrays?

Regards

Because static array aren't ranges, but dynamic ones are. Try this:
writeln("a1" ", sum (a1[])); // using [] makes static array to be a range
```
Dec 06 2015
cym13 <cpicard openmailbox.org> writes:
```On Sunday, 6 December 2015 at 12:23:05 UTC, Tim K. wrote:
Hi! I have the following code:

int main(string[] argv)
{
import std.algorithm: sum;
import std.stdio: writeln;

uint[3] a1 = [1, 2, 3];
uint[] a2;
for (int i = 1; i <= 3; ++i)
a2 ~= i;

writeln("a1: ", sum(a1));
writeln("a2: ", sum(a2));
return 0;
}

This throws the error:
dummy.d(11): Error: template std.algorithm.iteration.sum cannot
deduce function from argument types !()(uint[3]), candidates
are:
/usr/include/dmd/phobos/std/algorithm/iteration.d(3916):
std.algorithm.iteration.sum(R)(R r) if (isInputRange!R &&
!isInfinite!R && is(typeof(r.front + r.front)))
/usr/include/dmd/phobos/std/algorithm/iteration.d(3927):
std.algorithm.iteration.sum(R, E)(R r, E seed) if
(isInputRange!R && !isInfinite!R && is(typeof(seed = seed +
r.front)))

So a dynamic array works just fine. In fact, if I uncomment the
line in question the program compiles and outputs the correct
value (for a2). Why does "sum" not work on static arrays?

Regards

So that you do not shoot yourself in the foot too easily. A
static array is a value type so it is passed by value to
functions. If you pass a 1M array to a function... well, I guesse
you don't want to do that.

The solution is to slice it:   a1[].sum;   That way you avoid the
problem.
```
Dec 06 2015
Tim K. <invalid webinterface.com> writes:
```On Sunday, 6 December 2015 at 12:27:49 UTC, cym13 wrote:
A static array is a value type so it is passed by value to
functions.

Oh, right, I totally forgot about that. Thank you for reminding
me.
And yes, I was not planning on doing that. I just have a local
fixed-size array that I wanted to get the sum of...

The solution is to slice it:   a1[].sum;   That way you avoid
the problem.

Thank you two.

Regards
```
Dec 06 2015