Thread overview
[Issue 7227] New: [] syntax for empty associative array too?
Mar 11, 2013
Ivan Kazmenko
Jun 30, 2013
Henning Pohl
[Issue 7227] [:] as empty associative array literal, plus warning for null
January 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7227

           Summary: [] syntax for empty associative array too?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-01-04 14:37:27 PST ---
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([:]);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 11, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227


Ivan Kazmenko <gassa@mail.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gassa@mail.ru


--- Comment #1 from Ivan Kazmenko <gassa@mail.ru> 2013-03-11 14:14:59 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 30, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227


Henning Pohl <henning@still-hidden.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
                 CC|                            |henning@still-hidden.de


--- Comment #2 from Henning Pohl <henning@still-hidden.de> 2013-06-30 15:32:15 PDT ---
https://github.com/D-Programming-Language/dmd/pull/2284

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 01, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227



--- Comment #3 from bearophile_hugs@eml.cc 2013-06-30 17:03:20 PDT ---
(In reply to comment #2)
> https://github.com/D-Programming-Language/dmd/pull/2284

This patch has chosen the [] syntax over the [:] syntax.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 01, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227



--- Comment #4 from bearophile_hugs@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: -------
July 02, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227



--- Comment #5 from bearophile_hugs@eml.cc 2013-07-02 00:33:38 PDT ---
(In reply to comment #3)

> This patch has chosen the [] syntax over the [:] syntax.

But I prefer the [:] syntax, because it's more precise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 02, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227



--- Comment #6 from bearophile_hugs@eml.cc 2013-07-02 00:46:28 PDT ---
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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 03, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7227


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[:] as empty associative    |[:] as empty associative
                   |array literal               |array literal, plus warning
                   |                            |for null


--- Comment #7 from bearophile_hugs@eml.cc 2013-07-03 09:57:18 PDT ---
I have opened a discussion here:

http://forum.dlang.org/thread/rkdzdxygpflpnaznxxnl@forum.dlang.org

In the discussion I have explained better why I think [:] is better than [].

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