I am playing around with D arrays and I stumbled upon this:
When I compile this code
import std.stdio;
int main()
{
int[] a, b;
b ~= [2,3,4,5];
a.length = b.length;
a[] = b[] + 4;
writeln(typeid(a),"\n",typeid(b));
writeln(a,"\n",b);
int[] c, d;
for (int n=10; n<20; ++n) {
d ~= n;
}
c.length = d.length;
c = d[] + 2; //compile error
writeln(typeid(c),"\n",typeid(d));
writeln(c,"\n",d);
return 0;
}
I get an error at compilation:
Error: Array operation d[] + 2 not implemented
Ht only difference between the first few lines and the rest is the way b and d are filled.
How can I use array operations with an array filled in a loop?
And another question:
Are the first few lines the right way to do it? The line a.length = b.length; seems a little odd to me.
First question: You just forgot
[]on the left side.c[] = d[] + 2;works.Second question: Yes; vector operations need arrays of equal size, and setting
.lengthis one way to resize an array.