The inconsistencies presented in this ideone sample, are very hard to understand. There documentation is also very lacking on the behaviour of these operations.
It also appears there can be no temporaries with array operations?
Code: http://ideone.com/rEDVc
Comment #1 by daniel350 — 2012-03-24T02:02:54Z
Incase the ideone link expires:
#!/usr/bin/rdmd
import std.stdio;
void main() {
float[3] x = [1,1,1]; // ok
float[] y = [5,5,5,5]; // ok
writeln(x[] + x[]); // error (Array operation v1[] + v1[] not implemented)
float[] t1 = x[] + x[]; // ""
float[] t2 = y[] + y[]; // ""
auto t3 = x[] + x[]; // ""
float[] t1b = x[]; // ok
t1b[] += x[]; // still ok
float[3] t2b = y[] + y[]; // ok??? how does this even... appears to give a float[3] array with values of a [0..3] slice of y[] + y[]...
writeln(t2b);
auto t3b = x[]; // ok
t3b[] += x[]; // still ok
writeln(t3b);
x[] = x[] + 4; // ok
writeln(x);
x[] = x[] + x[]; // ok
writeln(x);
y[] = y[] + 4; // ok
writeln(y);
y[] = y[] + y[]; // ok
writeln(y);
float t4[3];
t4 = x[] + y[]; //ok
writeln(t4);
}
Comment #2 by daniel350 — 2012-03-24T02:13:16Z
Its cases like this that are curious aswell.
void main() {
int[] a = [3,3,3];
int[2] y = a[] + a[]; // ok
int[2] x = [3,3,3] + [3,3,3]; // error
writeln(y);
}
Comment #3 by daniel350 — 2012-03-24T02:27:57Z
If nothing else, the following must be a compiler bug, or otherwise a very unclear syntax;
int[2] fnc(int[2] a, int[2] b) {
return a[] + b[]; // Error: cannot implicitly convert expression (a[] + b[]) of type int[] to int[2LU]
}
int[2] fnc2(int[2] a, int[2] b) {
int[2] x = a[] + b[];
return x; // ok
}
regards,
Daniel :)
Comment #4 by daniel350 — 2012-03-24T02:50:38Z
Possible duplicate of things pointed out in #3066
Comment #5 by k.hara.pg — 2015-06-04T05:14:15Z
With 2.067:
(In reply to daniel from comment #1)
> writeln(x[] + x[]); // error (Array operation v1[] + v1[] not
> implemented)
> float[] t1 = x[] + x[]; // ""
> float[] t2 = y[] + y[]; // ""
> auto t3 = x[] + x[]; // ""
The produced error message is finally improved as:
Error: array operation x[] + x[] without destination memory not allowed
In above code, you're trying to get "slice" of the result of array operations, but array operations don't allocate new array to store the calculated elements. Therefore such the impossible code will be rejected and error messages explain the requirement.
> float[3] t2b = y[] + y[]; // ok??? how does this even... appears to
> give a float[3] array with values of a [0..3] slice of y[] + y[]...
> writeln(t2b);
The length of y will be asserted in runtime to fit to the result length of array operation result. In this case it's 3, that's same with the destination memory length.
(In reply to daniel from comment #2)
> int[] a = [3,3,3];
> int[2] y = a[] + a[]; // ok
> int[2] x = [3,3,3] + [3,3,3]; // error
Today both lines will fail at runtime. Maybe that was an already fixed druntime bug.
(In reply to daniel from comment #3)
> If nothing else, the following must be a compiler bug, or otherwise a very
> unclear syntax;
>
> int[2] fnc(int[2] a, int[2] b) {
> return a[] + b[]; // Error: cannot implicitly convert expression (a[] +
> b[]) of type int[] to int[2LU]
> }
It's a dupe of issue 12648.
Finally the "inconsistencies" are
- fixed as design
- fixed as a druntime bug
- remained bug 12648
So I close this issue as "fixed".