You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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[])
The text was updated successfully, but these errors were encountered:
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 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.
(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()])
bearophile_hugs reported this on 2013-08-24T08:55:18Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=10880
CC List
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[])The text was updated successfully, but these errors were encountered: