[Issue 238] New: Cannot initialise const field from foreach loop on associative array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 3 11:53:43 PDT 2006


http://d.puremagic.com/issues/show_bug.cgi?id=238

           Summary: Cannot initialise const field from foreach loop on
                    associative array
           Product: D
           Version: 0.162
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: minor
          Priority: P3
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: deewiant at gmail.com


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.


-- 




More information about the Digitalmars-d-bugs mailing list