www.digitalmars.com         C & C++   DMDScript  

D - Bug in foreach with assoc arrays.

reply Evan <1 3.com> writes:
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

I seem to have run into a bug in the foreach statement.  This comes 
originally from some work that I've been doing wrapping socket and 
porting over Python's asyncore module, but it could apply more widely, 
but I haven't really had time to explore it more fully.

The problem is this (also see the attached code):

When you use a foreach statement with an associative array, it won't 
compile cleanly if your second index is a typedef'd uint (ex. a SOCKET). 
  This also casts into doubt whether it will properly iterate over the 
assoc array in that circumstance, but I worked around it because I 
needed it to compile (changed typedef to alias).  I'll follow this up 
after I've done some more testing to see if it's getting iterated over 
correctly.

Thanks,

Evan
Feb 20 2004
parent reply "Walter" <walter digitalmars.com> writes:
I think the example mixes up what the key and the value is in the foreach.
The first is the key, the second is the value. I think the example should be
written instead:

typedef uint other;

char[other] map;

char[] remap;

void main()
{
    remap.length = map.length;

    int i;
    foreach(char c; map)
    {
          remap[i] = c;
          i++;
    }
}

which works.

"Evan" <1 3.com> wrote in message news:c15tr9$pho$1 digitaldaemon.com...
 I seem to have run into a bug in the foreach statement.  This comes
 originally from some work that I've been doing wrapping socket and
 porting over Python's asyncore module, but it could apply more widely,
 but I haven't really had time to explore it more fully.

 The problem is this (also see the attached code):

 When you use a foreach statement with an associative array, it won't
 compile cleanly if your second index is a typedef'd uint (ex. a SOCKET).
   This also casts into doubt whether it will properly iterate over the
 assoc array in that circumstance, but I worked around it because I
 needed it to compile (changed typedef to alias).  I'll follow this up
 after I've done some more testing to see if it's getting iterated over
 correctly.

 Thanks,

 Evan

---------------------------------------------------------------------------- ----
 typedef uint other;

 char[other] map;

 char[] remap;

 void main()
 {
 remap.length = map.length;

 foreach(uint i, other o; map)
 {
 remap[i] = map[o];
 }
 }

Feb 22 2004
parent C <dont respond.com> writes:
Scary to see an 'i' unitialized ( have the compiler do it ) lol , its be=
en =

beaten into my brain!

C

On Sun, 22 Feb 2004 14:15:35 -0800, Walter <walter digitalmars.com> wrot=
e:

 I think the example mixes up what the key and the value is in the =

 foreach.
 The first is the key, the second is the value. I think the example =

 should be
 written instead:

 typedef uint other;

 char[other] map;

 char[] remap;

 void main()
 {
     remap.length =3D map.length;

     int i;
     foreach(char c; map)
     {
           remap[i] =3D c;
           i++;
     }
 }

 which works.

 "Evan" <1 3.com> wrote in message news:c15tr9$pho$1 digitaldaemon.com.=

 I seem to have run into a bug in the foreach statement.  This comes
 originally from some work that I've been doing wrapping socket and
 porting over Python's asyncore module, but it could apply more widely=


 but I haven't really had time to explore it more fully.

 The problem is this (also see the attached code):

 When you use a foreach statement with an associative array, it won't
 compile cleanly if your second index is a typedef'd uint (ex. a SOCKE=


   This also casts into doubt whether it will properly iterate over th=


 assoc array in that circumstance, but I worked around it because I
 needed it to compile (changed typedef to alias).  I'll follow this up=


 after I've done some more testing to see if it's getting iterated ove=


 correctly.

 Thanks,

 Evan

----------------------------------------------------------------------=

 ----


 typedef uint other;

 char[other] map;

 char[] remap;

 void main()
 {
 remap.length =3D map.length;

 foreach(uint i, other o; map)
 {
 remap[i] =3D map[o];
 }
 }


-- = Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Feb 23 2004