www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5635] New: Code inside 'foreach' cannot modify variable out of its scope in a template function.

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

           Summary: Code inside 'foreach' cannot modify variable out of
                    its scope in a template function.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: kennytm gmail.com



The following program should compile:

pure bool foo(R=int)(string x) {
  bool result = false;
  foreach (dchar d; x)
    result = true;
  return result;
}

void main () {
  foo("hi");
}

But as of v2.052 it raises the errors:

x.d(4): Error: pure nested function '__foreachbody3' cannot access mutable data
'result'
x.d(9): Error: template instance x.foo!(int) error instantiating

If the template parameter (R=int) were removed, the program compiles
successfully. (I think this is related to Issue 3316).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011