Thread overview
Bug in foreach with assoc arrays.
Feb 20, 2004
Evan
Feb 22, 2004
Walter
Feb 23, 2004
C
February 20, 2004
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


February 22, 2004
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];
> }
> }


February 23, 2004
Scary to see an 'i' unitialized ( have the compiler do it ) lol , its been beaten into my brain!

C

On Sun, 22 Feb 2004 14:15:35 -0800, Walter <walter@digitalmars.com> wrote:

> 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];
>> }
>> }
>
>



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