D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12757 - Refused fixed size array literal function argument
Summary: Refused fixed size array literal function argument
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on: 4703
Blocks:
  Show dependency treegraph
 
Reported: 2014-05-16 19:20 UTC by bearophile_hugs
Modified: 2024-12-13 18:20 UTC (History)
2 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 2014-05-16 19:20:17 UTC
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 yebblies 2014-05-17 07:56:39 UTC
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 bearophile_hugs 2014-05-17 08:14:04 UTC
(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 yebblies 2014-05-17 08:25:58 UTC
(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 yebblies 2014-05-17 08:27:41 UTC
If this was implemented, this call would become ambiguous.

void foo(int[1]) {}
void foo(int[int]) {}

void main()
{
    foo([0 : 1]);
}
Comment 5 bearophile_hugs 2014-05-18 08:39:52 UTC
(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 dlangBugzillaToGithub 2024-12-13 18:20:41 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18829

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB