Comment #0 by bearophile_hugs — 2012-01-04T14:37:27Z
void foo(int[int] bb) {}
void main() {
int[int] aa;
aa = null; // OK
foo(null); // OK
aa = []; // Error
foo([]); // Error
}
DMD 2.058head gives:
test.d(6): Error: cannot implicitly convert expression ([]) of type void[] to int[int]
test.d(7): Error: function test2.foo (int[int] bb) is not callable using argument types (void[])
test.d(7): Error: cannot implicitly convert expression ([]) of type void[] to int[int]
I am not sure, but maybe the second syntax too should be accepted.
An alternative syntax for empty associative array literal:
void foo(int[int] bb) {}
void main() {
int[int] aa;
aa = [:];
foo([:]);
}
Comment #1 by gassa — 2013-03-11T14:14:59Z
I agree that an empty associative array could use a more convenient syntax.
Consider having an associative array of associative arrays, like in the following example:
-----
import std.stdio;
void main ()
{
int [int] [int] f;
assert (f.length == 0);
writeln (f); // []
f[5] = null;
assert (f.length == 1);
assert (f[5].length == 0);
writeln (f); // [5:[]]
}
-----
Now, for a D newbie like me, the line "f[5] = null;" intuitively looks more like a "delete f[5] from f if it is present" than an "add an empty f[5] into f". A syntax like "f[5] = [];" or "f[5] = [:];" would certainly improve readability here.
Comment #4 by bearophile_hugs — 2013-07-01T03:01:04Z
yebblies commented on GitHub:
> Ideally this would not be the same as K[V] aa = null;,
> it would behave like K[V] aa = new K[V]; - an AA would be allocated.
I think this is a bad idea, because then the semantics of D code changes if you use [] instead of null. D associative arrays have troubles:
void test(int[int] arraya, int x) {
arraya[x] = x;
}
void main() {
int[int] d;
test(d, 0);
int[int] d0;
assert(d == d0); // d is empty, 0:0 is lost
d[1] = 1;
test(d, 2);
assert(d == [1: 1, 2: 2]); // now 2:2 is not lost
}
Compared to the output of this Python code:
def test(arraya, x):
arraya[x] = x
def main():
d = {}
test(d, 0)
assert d == {0: 0}
d[1] = 1
test(d, 2)
assert d == {0: 0, 1: 1, 2: 2}
main()
Such problems should be faced in other ways. Making the associative array literal semantics even more complex is not helping.
Comment #5 by bearophile_hugs — 2013-07-02T00:33:38Z
(In reply to comment #3)
> This patch has chosen the [] syntax over the [:] syntax.
But I prefer the [:] syntax, because it's more precise.
Comment #6 by bearophile_hugs — 2013-07-02T00:46:28Z
Having two obvious syntaxes to do the same thing is not so good. So I suggest to also introduce a warning for the usage of "null" as associative array literal:
void foo(int[int]) {}
void main() {
foo(null);
int[int][] aas = [null];
aas[0] = [1: 2, 2: 3];
}
=>
test.d(3): Warning: explicit [:] empty associative array literal is better than null, that will be deprecated
test.d(4): Warning: explicit [:] empty associative array literal is better than null, that will be deprecated
(The wording of such warning message is modelled on another warning message: test.d(3): Warning: explicit element-wise assignment (a)[] = 2 is better than a = 2)
(This warning is not meant to be kept. Later this warning is meant to become a deprecation, and later an error).
Comment #7 by bearophile_hugs — 2013-07-03T09:57:18Z