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

foreach on matrix literal refuses immutable iteration #18658

Open
dlangBugzillaToGithub opened this issue Aug 24, 2013 · 3 comments
Open

foreach on matrix literal refuses immutable iteration #18658

dlangBugzillaToGithub opened this issue Aug 24, 2013 · 3 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-08-24T08:55:18Z

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

CC List

  • Meta
  • yebblies

Description

A matrix literal can be assigned to an immutable, but this isn't done in a foreach:


void main() {
    immutable m1 = [[1]]; // OK
    foreach (immutable v; [[1]]) {} // error
}


dmd 2.064alpha gives:

test.d(3): Error: cannot implicitly convert expression (__aggr4[__key5]) of type int[] to immutable(int[])
@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 2013-11-20T07:44:52Z

And nor should it.  As the error says, [[1]] has type int[][], and you can't implicitly convert int[] to immutable(int[]).

Asking for initializer-style type inference is an enhancement, and doesn't work particularly well with the way foreach expands to for.

Alternatives are to use const, or give the aggregate expression an explicit type.

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 2013-11-20T08:07:34Z

(In reply to comment #1)

> Asking for initializer-style type inference is an enhancement,

OK.


> and doesn't work
> particularly well with the way foreach expands to for.

If you think this enhancement request is too much work to implement, or it causes other troubles, then close this ER down.

@dlangBugzillaToGithub
Copy link
Author

monkeyworks12 commented on 2013-11-20T14:25:52Z

(In reply to comment #1)
> And nor should it.  As the error says, [[1]] has type int[][], and you can't
> implicitly convert int[] to immutable(int[]).
> 
> Asking for initializer-style type inference is an enhancement, and doesn't work
> particularly well with the way foreach expands to for.
> 
> Alternatives are to use const, or give the aggregate expression an explicit
> type.

In this case, isn't [[1]] a unique expression, so it can be implicitly cast to immutable? Also, foreach (const v; [[1]]) works, but this doesn't (and it probably should):

class Test
{
}

foreach (const v; [new Test()])

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