//struct Bla {}
alias int Bla;
import std.stdio;
void main()
{
immutable(Bla[]) _a;
writeln(_a.length); // prints 0
// _a.length = 5; // Error: variable _a cannot modify immutable
immutable(Bla)[] a = _a;
a.length = 5; // compiles, but:
writeln(_a.length); // prints 0
immutable(Bla[string]) _files;
immutable(Bla)[string] files = _files;
}
Comment #1 by timon.gehr — 2011-11-21T08:30:21Z
That works as designed. I do not understand what you would expect it to do.
immutable(Bla[]) _a; // this is an array that cannot be changed
immutable(Bla)[] a; // this is an array whose elements cannot be changed
a = _a; // fine. a is now a slice to _a's data
a.length = 5; // reallocate storage for a.
assert(_a.length == 0); // length of _a does not change as it is a different variable
Comment #2 by hoganmeier — 2011-11-21T08:43:41Z
Yeah, was a subtle error in reasoning.
But what about the assoc. array case?
immutable(Bla[string]) _files = ["a":1, "b":2, "c":3];
immutable(Bla)[string] files = _files; // works
files["d"] = 5; // Error: files["d"] isn't mutable
writeln(_files);
writeln(files);
gdc rejects the assignment.
Ok, I see. I change the title to reflect the actual bug.
Comment #5 by k.hara.pg — 2011-12-18T06:30:45Z
I think there are at least two issues.
void main()
{
alias int Bla;
immutable(Bla[string]) ifiles = ["a":1, "b":2, "c":3];
immutable(Bla)[string] files = ifiles; // (1)
ifiles.remove ("a"); // (2)
}
1. Implicitly conversion from immutable to mutable AA reference.
2. Call 'remove' from immutable AA reference.
Comment #6 by bearophile_hugs — 2011-12-18T07:39:16Z
A related problem (I think I have already put this in Bugzilla, but I don't remember the issue number):
void main() {
int[char[]] aa; // line 2
char[] a1 = "hello".dup;
aa[a1] = 1; // line 4
}
It's stupid for D language to accept the line 2 and then refuse line 4 with:
test.d(4): Error: associative arrays can only be assigned values with immutable keys, not char[]
Line 2 too needs to become an error.
So a better error message is something like:
test.d(2): Error: built-in associative arrays accept only immutable keys, not char[]
Or alternatively:
test.d(2): Error: built-in associative arrays can be defined only with immutable keys, not char[]