Bug 13909 – A problem with immutable zip + assocArray
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-12-29T12:46:00Z
Last change time
2015-02-18T03:40:56Z
Keywords
diagnostic
Assigned to
peter.alexander.au
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-12-29T12:46:10Z
I don't understand what's going on here. The line where aa2 is initialized gets accepted, while the initialization of aa5 gets refused, despite in both cases they use zipped keys/values from immutable associative arrays:
import std.array: assocArray;
import std.range: zip;
immutable string[const string] aa1, aa2, aa4, aa5;
static this() pure {
immutable aa3 = ["a":"b"];
aa2 = aa3.byValue.zip(aa3.byKey).assocArray; // OK
aa4 = ["a":"b"];
aa5 = aa4.byValue.zip(aa4.byKey).assocArray; // Error
}
void main() {}
dmd 2.067alpha gives:
...\dmd2\src\phobos\std\array.d(281,11): Error: cannot modify const expression aa[t.__expand_field_0]
test.d(8,37): Error: template instance std.array.assocArray!(Zip!(Result, Result)) error instantiating
Comment #1 by peter.alexander.au — 2015-01-02T02:12:12Z
typeof(aa3) = immutable(string[string])
typeof(aa4) = immutable(string[const(string)])
You are swapping the order to (value, key) in your zip, so you are trying to create an associative array with const values (not keys), which isn't possible with assocArray.
Comment #2 by bearophile_hugs — 2015-01-02T08:14:10Z
(In reply to Peter Alexander from comment #1)
> so you are trying to create an associative array with const values
> (not keys), which isn't possible with assocArray.
I see. This code gives:
void main() {
const(string)[string] aa;
aa["a"] = "b";
}
test.d(3,7): Error: cannot modify const expression aa["a"]
So I change this issue a little, now it's a diagnostic problem.
Perhaps it's a good idea to add a template constraint to assocArray:
import std.traits: isMutable;
auto assocArray(Range)(Range r)
if (isInputRange!Range &&
ElementType!Range.length == 2 &&
isMutable!(ElementType!Range.Types[1]))
{
...
Comment #3 by peter.alexander.au — 2015-01-02T09:35:26Z