Bug 5255 – No foreach type inference with const

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-11-21T18:53:00Z
Last change time
2011-09-07T01:15:11Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-11-21T18:53:07Z
If in a D2 program I have an array of mutable items I may want to iterate on them but not modify them, so I'd like the iteration variable to be const. This is possible, but it seems I lose type inference: void main() { int[3] array; // not const // foreach (const x; array) {} // Error // foreach (const auto x; array) {} // Error // foreach (const(int) x; array) {} // OK foreach (const(typeof(array[0])) x; array) {} // OK } What's the correct syntax to use?
Comment #1 by yebblies — 2011-09-07T01:15:11Z
*** This issue has been marked as a duplicate of issue 4090 ***