On Friday, 11 September 2015 at 02:17:25 UTC, rcorre wrote:
I frequently find myself needing a data structure that maps each member of an enum to a value; something similar what Java calls an EnumMap (http://docs.oracle.com/javase/7/docs/api/java/util/EnumMap.html).

I couldn't find any D implementation out there, so I wrote a little module for it.
Enumap is available on Github (https://github.com/rcorre/enumap)
and via dub (http://code.dlang.org/packages/enumap).
Docs are hosted at http://rcorre.github.io/enumap/.


this looks excellent! I've been playing around with it, and am looking forward to using it regularly.

I've ran into a compilation error when iterating over a const Enumap. In the following code:

    import std.stdio;
    import std.conv;
    import enumap;

    enum MyEnum { e1, e2, e3 }

    struct A
        Enumap!(MyEnum, int) map;

        void mutable_output()
            foreach (MyEnum e, int i; map)
                writefln("%s: %d", e.to!string, i);

        void const_output() const
            foreach (MyEnum e, const int i; map)
                writefln("%s: %d", e.to!string, i);

...the first method (mutable_output) compiles and works with no errors. The const method, however, gives:

source/app.d(19,13): Error: invalid foreach aggregate this.map,
    define opApply(), range primitives, or use .tupleof".

It doesn't seem to matter whether I put const int, or int, in the foreach statement.

What's the idiomatic way to loop over a const Enumap? :-)

-- Simon

Reply via email to