Comment #0 by bearophile_hugs — 2010-08-21T10:24:52Z
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 by dsimcha — 2010-08-21T10:32:14Z
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 by nfxjfg — 2010-08-21T10:45:11Z
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 by dsimcha — 2010-08-21T12:51:30Z
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 by nfxjfg — 2010-08-21T13:04:06Z
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 by bearophile_hugs — 2010-08-21T13:36:39Z
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 by leandro.lucarella — 2010-08-21T17:23:13Z
(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 by andrej.mitrovich — 2010-08-21T17:59:50Z
(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 by leandro.lucarella — 2010-08-21T22:05:28Z
(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 by andrej.mitrovich — 2010-08-22T07:26:10Z
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 by andrej.mitrovich — 2012-03-23T15:54:17Z
Comment #11 by andrej.mitrovich — 2012-03-23T15:54:41Z
(In reply to comment #10)
> I think this bug
s/bug/syntax issue
Comment #12 by bearophile_hugs — 2013-02-16T07:24:19Z
See also Issue 9520
Comment #13 by andrej.mitrovich — 2013-02-16T08:36:41Z
(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 by bearophile_hugs — 2013-02-16T08:41:53Z
(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 by bearophile_hugs — 2014-05-16T19:20:27Z
See also Issue 12757
Comment #16 by robert.schadek — 2024-12-13T17:52:58Z