[Issue 7227] [] syntax for empty associative array too?

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 1 03:01:06 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=7227



--- Comment #4 from bearophile_hugs at eml.cc 2013-07-01 03:01:04 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list