Enumap -- a lightweight AA alternative when your keys are enums

SimonN via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Sep 10 21:30:30 PDT 2015


On Friday, 11 September 2015 at 04:02:17 UTC, rcorre wrote:
> On Friday, 11 September 2015 at 03:25:58 UTC, SimonN wrote:
>> Hi,
>> I've ran into a compilation error when iterating over a const 
>> Enumap. In the following code:
>
> Interesting, thanks for pointing that out.
> I don't think I did a great job with const-correctness here, 
> I'll take a look tomorrow.
> It should definitely be possible to iterate over (and index, 
> etc...) a const/immutable Enumset, though you're right that it 
> doesn't work right now.

No worries, take your time! Thanks for the quick clarification.

I've also tested a couple ways of assigning in a foreach. 
Continuing from my code above (struct A { Enumap!(MyEnum, int) 
map; /* ... */ }), I've tried this in the main function:

     int some_value = 100;
     A a;

     foreach (MyEnum e, ref val; a.map)
         val = ++some_value;
     a.mutable_output();

     foreach (MyEnum e, ref val; a.map)
         a.map[e] = ++some_value;
     a.mutable_output();

Output:

     e1: 0
     e2: 0
     e3: 0
     e1: 104
     e2: 105
     e3: 106

Since I have been using "ref val" in the first loop, I expected 
the output to be instead:

     e1: 101
     e2: 102
     e3: 103
     e1: 104
     e2: 105
     e3: 106

-- Simon


More information about the Digitalmars-d-announce mailing list