Comment #0 by bearophile_hugs — 2014-05-16T19:20:17Z
void foo(int[2] data) {}
void main() {
int[2] aux = [0: 10, 1: 20]; // OK
foo(aux); // OK
foo([0: 10, 1: 20]); // Error.
}
dmd 2.066alpha gives:
temp.d(5,8): Error: function temp2.foo (int[2] data) is not callable using argument types (int[int])
A very similar but more specific and more useful example:
import std.traits: EnumMembers;
enum Foo : ubyte { A, B }
void bar(int[EnumMembers!Foo.length] input) {}
void main() {
immutable int[EnumMembers!Foo.length] input = [Foo.A: 10, Foo.B: 20];
bar(input); // OK.
bar([Foo.A: 10, Foo.B: 20]); // Error.
}
See also:
https://issues.dlang.org/show_bug.cgi?id=4703
Comment #1 by yebblies — 2014-05-17T07:56:39Z
That's not an array literal, that's either an array initializer or an associative array literal, depending on context. You can't use an array initializer except when initializing a variable.
Comment #2 by bearophile_hugs — 2014-05-17T08:14:04Z
(In reply to yebblies from comment #1)
> That's not an array literal, that's either an array initializer or an
> associative array literal, depending on context. You can't use an array
> initializer except when initializing a variable.
I see. So is this request INVALID and to be closed?
Comment #3 by yebblies — 2014-05-17T08:25:58Z
(In reply to bearophile_hugs from comment #2)
> (In reply to yebblies from comment #1)
> > That's not an array literal, that's either an array initializer or an
> > associative array literal, depending on context. You can't use an array
> > initializer except when initializing a variable.
>
> I see. So is this request INVALID and to be closed?
Either that or turn it into an enhancement to allow array initializers to be used in any array-accepting context instead of just assignment.
Comment #4 by yebblies — 2014-05-17T08:27:41Z
If this was implemented, this call would become ambiguous.
void foo(int[1]) {}
void foo(int[int]) {}
void main()
{
foo([0 : 1]);
}
Comment #5 by bearophile_hugs — 2014-05-18T08:39:52Z
(In reply to yebblies from comment #4)
> If this was implemented, this call would become ambiguous.
>
> void foo(int[1]) {}
> void foo(int[int]) {}
>
> void main()
> {
> foo([0 : 1]);
> }
Right. And adding more ambiguous code to D is not a good idea.
Walter doesn't seem in a rush to find a fix for my Issue 4703 that is almost four years old. If some kind of fix for issue 4703 is ever found and accepted, then this enhancement request becomes acceptable.
So I tag this as enhancement request that depends on Issue 4703 .
Comment #6 by robert.schadek — 2024-12-13T18:20:41Z