Bug 238 – Cannot initialise const field from foreach loop on associative array
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-07-03T13:53:00Z
Last change time
2014-02-15T13:21:09Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla
Comments
Comment #0 by matti.niemenmaa+dbugzilla — 2006-07-03T13:53:42Z
This is a bit confusing. The code explains it best:
--
class Confectionary {
this(int sugar) {
//if (sugar < 500)
// tastiness = 200;
//for (int i = 0; i < 10; ++i)
// tastiness = 300;
//int[] tastinesses_array;
//foreach (n; tastinesses_array)
// tastiness = n;
int[int] tastinesses_aa;
foreach (n; tastinesses_aa)
tastiness = n;
}
const int tastiness;
}
--
All the commented-out cases are acceptable ways of initialising the const member, tastiness. However, DMD issues an error "can only initialize const tastiness inside constructor" for the last foreach loop.
This can be circumvented by putting the loop in a function which returns the value to be assigned to the field, but it's still an annoyance.
Comment #1 by matti.niemenmaa+dbugzilla — 2006-12-24T03:45:33Z