www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7227] New: [] syntax for empty associative array too?

reply d-bugmail puremagic.com writes:
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



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: -------
Jan 04 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7227


Ivan Kazmenko <gassa mail.ru> changed:

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



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: -------
Mar 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jun 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7227





 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: -------
Jun 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7227




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: -------
Jul 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7227






 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: -------
Jul 02 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7227




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: -------
Jul 02 2013
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jul 03 2013