[Issue 4703] New: Ambiguously designed array syntax
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Aug 21 10:24:55 PDT 2010
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 at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at 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: -------
More information about the Digitalmars-d-bugs
mailing list