Thread overview
Bad length in value of T[a..b] = scalar
Sep 01, 2004
Stewart Gordon
Sep 01, 2004
Nick
Sep 01, 2004
Stewart Gordon
Sep 01, 2004
Nick
Sep 01, 2004
Stewart Gordon
Sep 01, 2004
Nick
September 01, 2004
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.
September 01, 2004
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


September 01, 2004
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.
September 01, 2004
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


September 01, 2004
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.
September 01, 2004
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