D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7759 - Inconsistencies with Array Operations
Summary: Inconsistencies with Array Operations
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-24 01:56 UTC by daniel
Modified: 2015-06-04 05:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description daniel 2012-03-24 01:56:15 UTC
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 daniel 2012-03-24 02:02:54 UTC
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 daniel 2012-03-24 02:13:16 UTC
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 daniel 2012-03-24 02:27:57 UTC
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 daniel 2012-03-24 02:50:38 UTC
Possible duplicate of things pointed out in #3066
Comment 5 Kenji Hara 2015-06-04 05:14:15 UTC
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".