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

2022-12-17 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=12492

Iain Buclaw  changed:

   What|Removed |Added

   Priority|P2  |P3

--


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

2016-11-15 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=12492

Ali Cehreli  changed:

   What|Removed |Added

 CC||acehr...@yahoo.com

--- Comment #1 from Ali Cehreli  ---
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

--