Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 21, 2010 [Issue 4703] New: Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
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 --- Comment #0 from bearophile_hugs@eml.cc 2010-08-21 10:24:52 PDT --- 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: ------- |
August 21, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 David Simcha <dsimcha@yahoo.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |dsimcha@yahoo.com --- Comment #1 from David Simcha <dsimcha@yahoo.com> 2010-08-21 10:32:14 PDT --- 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: ------- |
August 21, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 nfxjfg@gmail.com changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |nfxjfg@gmail.com --- Comment #2 from nfxjfg@gmail.com 2010-08-21 10:45:11 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). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
August 21, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 --- Comment #3 from David Simcha <dsimcha@yahoo.com> 2010-08-21 12:51:30 PDT --- 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: ------- |
August 21, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 --- Comment #4 from nfxjfg@gmail.com 2010-08-21 13:04:06 PDT --- 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: ------- |
August 21, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 --- Comment #5 from bearophile_hugs@eml.cc 2010-08-21 13:36:39 PDT --- 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: ------- |
August 22, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 Leandro Lucarella <llucax@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |llucax@gmail.com --- Comment #6 from Leandro Lucarella <llucax@gmail.com> 2010-08-21 17:23:13 PDT --- (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". -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
August 22, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | 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 --- Comment #7 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-21 17:59:50 PDT --- (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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
August 22, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 --- Comment #8 from Leandro Lucarella <llucax@gmail.com> 2010-08-21 22:05:28 PDT --- (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 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
August 22, 2010 [Issue 4703] Ambiguously designed array syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4703 --- Comment #9 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-22 07:26:10 PDT --- 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 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation