www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Bad length in value of T[a..b] = scalar

reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Using DMD 0.100, Windows 98SE.

When a slice is assigned by a scalar, the value of the assignment 
expression has the length of the whole array, not that of the slice.

----------
import std.stdio;

void show(int[] s) {
     foreach (int i; s) {
         writef("%d ", i);
     }
     writefln();
}

void main() {
     int[] qwert = new int[6];
     int[] yuiop;
     yuiop = qwert[2..5] = 3;
     show(yuiop);
     show(qwert[2..5] = 4);
     show(qwert[2..5]);
     show(qwert);
     show(yuiop[2..5] = qwert[1..4]);

     yuiop = qwert[2..5];
     show(yuiop[1..3] = 6);
     writefln((yuiop[1..3] = 7).length);
}
----------

Output:

3 3 3 0 0 0
4 4 4 0 0 0
4 4 4
0 0 4 4 4 0
0 4 4
6 6 4
3

Expected output:

3 3 3
4 4 4
4 4 4
0 0 4 4 4 0
0 4 4
6 6
2

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.
Sep 01 2004
parent reply Nick <Nick_member pathlink.com> writes:
In article <ch44o1$211s$1 digitaldaemon.com>, Stewart Gordon says...
Using DMD 0.100, Windows 98SE.

When a slice is assigned by a scalar, the value of the assignment 
expression has the length of the whole array, not that of the slice.
I don't agree that this is a bug. a[2..5] = 5 is an assignment, not a slice. The result of the assignment is the old array a with elements 2,3,4 set to 5, which is what should be returned. Nick
Sep 01 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Nick wrote:

 In article <ch44o1$211s$1 digitaldaemon.com>, Stewart Gordon says...
 
 Using DMD 0.100, Windows 98SE.
 
 When a slice is assigned by a scalar, the value of the assignment 
 expression has the length of the whole array, not that of the 
 slice.
I don't agree that this is a bug. a[2..5] = 5 is an assignment, not a slice. The result of the assignment is the old array a with elements 2,3,4 set to 5, which is what should be returned.
By your logic, the same would apply to a[3] = 5 or an assignment to a class/struct/union member. The whole point of AssignExpression being an rvalue is that qwert = yuiop = asdfg; is identical to yuiop = asdfg; qwert = yuiop; except that yuiop is evaluated only once. Anyway, your description isn't what the bug is doing. Look at the output from my testcase again. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Sep 01 2004
parent reply Nick <Nick_member pathlink.com> writes:
In article <ch4h5a$280v$1 digitaldaemon.com>, Stewart Gordon says...
The whole point of AssignExpression being an rvalue is that

     qwert = yuiop = asdfg;

is identical to

     yuiop = asdfg;
     qwert = yuiop;

except that yuiop is evaluated only once.
Ok, I can agree with that. But the docs say exactly nothing about this, so I'm not really sure what I would call expected behaviour here.
Anyway, your description isn't what the bug is doing.  Look at the 
output from my testcase again.
That looks like a bug, yep. It looks like it was intended to work the way you describe, but it doesn't. Nick
Sep 01 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Nick wrote:
<snip>
 Ok, I can agree with that. But the docs say exactly nothing about this, so I'm
 not really sure what I would call expected behaviour here.
<snip> You mean this doesn't count? http://www.digitalmars.com/d/expression.html#AssignExpression Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Sep 01 2004
parent Nick <Nick_member pathlink.com> writes:
In article <ch4nva$2alo$1 digitaldaemon.com>, Stewart Gordon says...
Nick wrote:
<snip>
 Ok, I can agree with that. But the docs say exactly nothing about this, so
 I'm not really sure what I would call expected behaviour here.
<snip> You mean this doesn't count? http://www.digitalmars.com/d/expression.html#AssignExpression
I was thinking something more specifically about arrays, but again I agree with you. The link you refer to should cover the array slice case as well. Nick
Sep 01 2004