Bug in the array set operator

Sjoerd van Leent svanleent at gmail.com
Sat Jun 24 10:34:21 PDT 2006


roumen schreef:
> I'm fairly certain that the code below should not work, but it compiles and
> works OK. And if I use the "correct" statement a[0..a.length-1] = 0; it does not
> set the last element.
> 
> import std.stdio;
> private int[5] a = [1, 2, 3, 4, 5];
> void main(char[][] arg)
> {
> foreach (int n; a)
> writef("%d ", n);
> writefln();
> 
> a[0..a.length] = 0;   // BUG: should be a.length-1
> 
> foreach (int n; a)
> writef("%d ", n);
> writefln();
> }
> 
> 

It's the correct behaviour. You have to see this as the mathematical 
expression: [a .. b)

In D, you can shorten your array statement as:

a[0..$] = 0;

Regards,
Sjoerd



More information about the Digitalmars-d mailing list