Bug 10880 – foreach on matrix literal refuses immutable iteration

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-24T08:55:18Z
Last change time
2024-12-13T18:10:52Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#18658 →

Comments

Comment #0 by bearophile_hugs — 2013-08-24T08:55:18Z
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[])
Comment #1 by yebblies — 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.
Comment #2 by bearophile_hugs — 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.
Comment #3 by monkeyworks12 — 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()])
Comment #4 by robert.schadek — 2024-12-13T18:10:52Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18658 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB