Bug 8737 – Associative Array (AA) KeyType is not Unqual-able
Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-09-29T15:42:00Z
Last change time
2015-06-09T01:31:15Z
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2012-09-29T15:42:26Z
There is an issue with AA's, where it is not possible to extract the exact "mutable KeyType" of an AA. This is a built in protection for when iterating on reference keys.
The problem is that it becomes impossible to create new keys, without knowing the "real" type.
This was revealed in a bug in conv.to : #8705 http://d.puremagic.com/issues/show_bug.cgi?id=8705
Reduced test:
--------
import std.stdio;
import std.traits;
import std.conv;
void main()
{
alias short[short[short]] S;
alias int[int[int]] T;
S value;
T result;
alias Unqual!(KeyType!T) K2; // const(int)[int]
alias Unqual!(ValueType!T) V2; // int
foreach (k1, v1; value)
{
K2 k2 = to2!K2(k1); //request a cast to "const(int)[int]" !!!
V2 v2 = to!V2(v1);
result[k2] = v2;
}
}
T to2(T, S)(S value) //T is const(int)[int]
{
alias Unqual!(KeyType!T) K2;
alias Unqual!(ValueType!T) V2;
Unqual!T result;
foreach (k1, v1; value)
{
K2 k2 = to!K2(k1); //const(int)
V2 v2 = to!V2(v1); //int
result[k2] = v2; //Error: result[k2] isn't mutable (naturally)
}
return result;
}
--------
Here, the implementer of "foo" is unable to tranform his keys, because their types are not mutable.
The real problem is that KeyType returned "const(int)[int]", is not really const, but not mutable either. Because of this Unqual doesn't work.
Suggestion: The type returned by KeyType should be "full const", eg:
const(int[int]).
This would make it just as safe, but a user can still request an Unqual on the type.
Comment #1 by monarchdodra — 2012-09-29T15:55:11Z
(In reply to comment #0)
> Here, the implementer of "foo" is unable to tranform his keys, because their
> types are not mutable.
I meant "to"
> The real problem is that KeyType returned "const(int)[int]", is not really
> const, but not mutable either. Because of this Unqual doesn't work.
>
> Suggestion: The type returned by KeyType should be "full const", eg:
> const(int[int]).
No, wait that is bad advice. Recommend changing "Unqual" actually;
--------
template Unqual(T)
if(!isAssociativeArray!T) //New condition
{
//As it was
}
//New overload
template Unqual(T)
if(isAssociativeArray!T)
{
alias KeyType!T K;
alias Unqual!(ValueType!T) V;
alias V[K] Unqual;
}
--------
I'll do this tommorrow, unless I get some objections?
Comment #2 by github-bugzilla — 2012-10-04T11:56:29Z