Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refused fixed size array literal function argument #18829

Open
dlangBugzillaToGithub opened this issue May 16, 2014 · 5 comments
Open

Refused fixed size array literal function argument #18829

dlangBugzillaToGithub opened this issue May 16, 2014 · 5 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2014-05-16T19:20:17Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12757

CC List

  • yebblies

Description

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
@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 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.

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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?

@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 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.

@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 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]);
}

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant