www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4703] New: Ambiguously designed array syntax

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703

           Summary: Ambiguously designed array syntax
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This is a wrong program, where's the bug?

import std.stdio: writeln;
void main() {
    int[] dict = [1:2, 3:4, 5:6];
    writeln(dict);
}


The output is:
[0, 2, 0, 4, 0, 6]


The problem is that the literal of 'aa' is both a valid dynamic array literal
and valid associative array literal. On default DMD chooses to see it as an
associative array, but this choice is arbitrary:

import std.stdio: writeln;
void main() {
    auto aa = [1:2, 3:4, 5:6];
    writeln(aa);
}


It's very bad to have ambiguous built-in collection literals, it's a source of
many problems.

And it's not even an uniform choice, with dmd 2.048 this program:

void foo(int[] a) {}
void bar(int[int] aa) {}
void main() {
    foo([1:2, 3:4, 5:6]);
    bar([1:2, 3:4, 5:6]);
}


Produces the errors:
test.d(4): Error: function test.foo (int[] a) is not callable using argument
types (int[int])
test.d(4): Error: cannot implicitly convert expression ([1:2,3:4,5:6]) of type
int[int] to int[]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha yahoo.com



This is definitely a real bug in some capacity or another, but can you explain
to the unenlightened how [1:2, 3:4, 5:6] could possibly be construed as valid
int[] literal syntax?  I would have seen this as a pure implementation bug, not
a spec ambiguity issue as you imply it is.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com



Look on http://www.digitalmars.com/d/2.0/arrays.html section "Static
Initialization of Static Arrays" (btw. Walter could you create an index of some
sort, so one can post direct links, or, you know, actually find stuff without
having to go through the whole damn spec? if ddoc wasn't shit, it'd do that
automatically).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




I think, then, that we should just get rid of the static initialization of
static arrays thing.  I've been using D on a daily basis for ~2.5 years and I
didn't know it existed.  I've never actually seen it used in any D code
anywhere.  IIRC it's not mentioned in TDPL, and it certainly creates a horrible
ambiguity.  If this feature is really that important, maybe it could be moved
to a library and handled with CTFE.  Here's a quick and dirty example of such a
function, which could be tested, fleshed out, etc.

auto staticInitializeStaticArray(T...)(T args) {
    static assert(args.length % 2 == 0);
    T[1][T.length / 2] ret;

    foreach(ti, arrIndex; args) {
        if(ti % 2 == 1) {
            continue;
        }

        ret[arrIndex] = args[ti + 1];
    }

    return ret;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




This ferature is in C99, with different syntax.
Example from the GCC docs:

int a[6] = { [4] = 29, [2] = 15 };

It's very useful. D's inability to initialize anything else than static
variables with this syntax and the upcoming of CTFE made it an mostly
meaningless, obscure feature.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




I agree that this syntax is not very useful, and it may be considered for
removal:
int[] dict = [1:2, 3:4, 5:6];


 This ferature is in C99, with different syntax.
 [...] made it an mostly meaningless, obscure feature.
In D there is other syntax that is left from C or adapted from C that gives problems: enum string[5] data = ["green", "magenta", "blue" "red", "yellow"]; static assert(data[4] == "yellow"); // asserts void main() { string s(); // what is this? int x1 = 066; // octal double[2] a = [1.0, 2.0]; double[2] b = [3.0, 4.0]; double[2] c[] = a[] + b[]; // silly error } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703


Leandro Lucarella <llucax gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |llucax gmail.com



PDT ---

 Look on http://www.digitalmars.com/d/2.0/arrays.html section "Static
 Initialization of Static Arrays" (btw. Walter could you create an index of some
 sort, so one can post direct links, or, you know, actually find stuff without
 having to go through the whole damn spec? if ddoc wasn't shit, it'd do that
 automatically).
I usually check the source of the page, many sections have an <a name=""> so you can create direct links if you **really** need them. Yes, I agree it sucks hard, an index would be the right thing to do, but maybe you didn't knew the "workaround". -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



17:59:50 PDT ---


 Look on http://www.digitalmars.com/d/2.0/arrays.html section "Static
 Initialization of Static Arrays" (btw. Walter could you create an index of some
 sort, so one can post direct links, or, you know, actually find stuff without
 having to go through the whole damn spec? if ddoc wasn't shit, it'd do that
 automatically).
I usually check the source of the page, many sections have an <a name=""> so you can create direct links if you **really** need them. Yes, I agree it sucks hard, an index would be the right thing to do, but maybe you didn't knew the "workaround".
That's nice, but you still can't make a URL out of that so others can view the exact title, right? I had this problem when I was doing a review of the spec, I had to put quotes around the title to easily find the section since there's a shortage of href links. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




PDT ---



 Look on http://www.digitalmars.com/d/2.0/arrays.html section "Static
 Initialization of Static Arrays" (btw. Walter could you create an index of some
 sort, so one can post direct links, or, you know, actually find stuff without
 having to go through the whole damn spec? if ddoc wasn't shit, it'd do that
 automatically).
I usually check the source of the page, many sections have an <a name=""> so you can create direct links if you **really** need them. Yes, I agree it sucks hard, an index would be the right thing to do, but maybe you didn't knew the "workaround".
That's nice, but you still can't make a URL out of that so others can view the exact title, right? I had this problem when I was doing a review of the spec, I had to put quotes around the title to easily find the section since there's a shortage of href links.
Yes you can, for example this is the link to the section mentioned by nfxjfg in comment 2: http://www.digitalmars.com/d/2.0/arrays.html#static-init-static -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




07:26:10 PDT ---
I did not know that, thanks! :)





 Look on http://www.digitalmars.com/d/2.0/arrays.html section "Static
 Initialization of Static Arrays" (btw. Walter could you create an index of some
 sort, so one can post direct links, or, you know, actually find stuff without
 having to go through the whole damn spec? if ddoc wasn't shit, it'd do that
 automatically).
I usually check the source of the page, many sections have an <a name=""> so you can create direct links if you **really** need them. Yes, I agree it sucks hard, an index would be the right thing to do, but maybe you didn't knew the "workaround".
That's nice, but you still can't make a URL out of that so others can view the exact title, right? I had this problem when I was doing a review of the spec, I had to put quotes around the title to easily find the section since there's a shortage of href links.
Yes you can, for example this is the link to the section mentioned by nfxjfg in comment 2: http://www.digitalmars.com/d/2.0/arrays.html#static-init-static
-- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 22 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




15:54:17 PDT ---
I think this bug is the cause of bug 6469. Discussed in
http://forum.dlang.org/thread/CAJ85NXDh47+0fNrykjRh04KfRCSjC=NSNzR8AgD2p3ZF2aE5pw mail.gmail.com#post-jkhg1u:242ou2:241:40digitalmars.com

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




15:54:41 PDT ---

 I think this bug
s/bug/syntax issue -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




See also Issue 9520

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703




08:36:41 PST ---

 See also Issue 9520
P.S. there is a "See Also" box at the top where you can put links to related issues. It will ensure your comment doesn't get lost or ignored. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 16 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4703






 P.S. there is a "See Also" box at the top where you can put links to related
 issues. It will ensure your comment doesn't get lost or ignored.
I didn't see it. Thank you. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 16 2013