[Issue 12492] [AA] Clarify what types can be used to get associative array key value

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Nov 15 00:28:46 PST 2016


https://issues.dlang.org/show_bug.cgi?id=12492

Ali Cehreli <acehreli at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |acehreli at yahoo.com

--- Comment #1 from Ali Cehreli <acehreli at yahoo.com> ---
I hit this problem in a const member function where the member type of .values
turned out to be const:

struct S {
    int[char[]] aa;

    void constFunc() const {
        static assert(is(typeof(aa.keys[0]) == const(char)[]));  // Passes,
good
        static assert(is(typeof(aa.values[0]) == int));          // FAILS
    }
}

void main() {
}

(In my particular case, I had attempted to sort the copy of values, which had
failed due to const(int) members.)

Note that the workaround of declaring a non-const array is totally safe:

        int[] values;
        foreach (v; aa.byValue) {
            values ~= v;
        }

Of course functional style with .byValue would still produce the wrong type:

        auto values = aa.byValue.array;
        static assert(is(typeof(values[0]) == int));  // FAILS

Ali

--


More information about the Digitalmars-d-bugs mailing list