Issue 4703 - Ambiguously designed array/AA literal syntax
Summary: Ambiguously designed array/AA literal syntax
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks: 12757
  Show dependency treegraph
 
Reported: 2010-08-21 10:24 UTC by bearophile_hugs
Modified: 2024-07-15 15:20 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-08-21 10:24:52 UTC
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[]
Comment 1 David Simcha 2010-08-21 10:32:14 UTC
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.
Comment 2 nfxjfg 2010-08-21 10:45:11 UTC
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).
Comment 3 David Simcha 2010-08-21 12:51:30 UTC
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;
}
Comment 4 nfxjfg 2010-08-21 13:04:06 UTC
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.
Comment 5 bearophile_hugs 2010-08-21 13:36:39 UTC
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
}
Comment 6 Leandro Lucarella 2010-08-21 17:23:13 UTC
(In reply to comment #2)
> 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".
Comment 7 Andrej Mitrovic 2010-08-21 17:59:50 UTC
(In reply to comment #6)
> (In reply to comment #2)
> > 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.
Comment 8 Leandro Lucarella 2010-08-21 22:05:28 UTC
(In reply to comment #7)
> (In reply to comment #6)
> > (In reply to comment #2)
> > > 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
Comment 9 Andrej Mitrovic 2010-08-22 07:26:10 UTC
I did not know that, thanks! :)

(In reply to comment #8)
> (In reply to comment #7)
> > (In reply to comment #6)
> > > (In reply to comment #2)
> > > > 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
Comment 10 Andrej Mitrovic 2012-03-23 15:54:17 UTC
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
Comment 11 Andrej Mitrovic 2012-03-23 15:54:41 UTC
(In reply to comment #10)
> I think this bug
s/bug/syntax issue
Comment 12 bearophile_hugs 2013-02-16 07:24:19 UTC
See also Issue 9520
Comment 13 Andrej Mitrovic 2013-02-16 08:36:41 UTC
(In reply to comment #12)
> 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.
Comment 14 bearophile_hugs 2013-02-16 08:41:53 UTC
(In reply to comment #13)

> 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.
Comment 15 bearophile_hugs 2014-05-16 19:20:27 UTC
See also Issue 12757