www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Bug in the array set operator

reply roumen <roumen_member pathlink.com> writes:
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();
}
Jun 22 2006
next sibling parent "Chris Miller" <chris dprogramming.com> writes:
On Thu, 22 Jun 2006 21:39:40 -0400, roumen <roumen_member pathlink.com> =
 =

wrote:

 I'm fairly certain that the code below should not work, but it compile=
s =
 and
 works OK. And if I use the "correct" statement a[0..a.length-1] =3D 0;=
it =
 does not
 set the last element.

 import std.stdio;
 private int[5] a =3D [1, 2, 3, 4, 5];
 void main(char[][] arg)
 {
 foreach (int n; a)
 writef("%d ", n);
 writefln();

 a[0..a.length] =3D 0;   // BUG: should be a.length-1

 foreach (int n; a)
 writef("%d ", n);
 writefln();
 }
It's [inclusive..exclusive], so [0..length] is the whole array and [0..0= ] = is empty.
Jun 22 2006
prev sibling parent reply Sjoerd van Leent <svanleent gmail.com> writes:
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
Jun 24 2006
next sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Sjoerd van Leent wrote:
 In D, you can shorten your array statement as:
 
 a[0..$] = 0;
or just: a[] = 0; -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 24 2006
prev sibling parent "Ameer Armaly" <ameer_armaly hotmail.com> writes:
"Sjoerd van Leent" <svanleent gmail.com> wrote in message 
news:e7jt2l$175u$1 digitaldaemon.com...
 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 short, saying [a..$] means "go up to but not including $," which means all elements. A bit confusing at first, but useful.
 In D, you can shorten your array statement as:

 a[0..$] = 0;

 Regards,
 Sjoerd 
Jun 24 2006