www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 251] New: foreach does not allow updating inside with block

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=251

           Summary: foreach does not allow updating inside with block
           Product: D
           Version: 0.162
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: ashleymedlock yahoo.com


Array members cannot be updated inside a with block for either structs or
classes.
Sample code below:

import std.stdio;


struct Bar { float a= 1.0; }

struct Foo
{
  Bar[]   arr;
}


void main( char[][] args )
{
  Foo foo ;//= new Foo();
  foo.arr.length = 20;

  with(foo)
  {
    foreach( int n, Bar bar; arr ) bar.a = 100;
  }
  foreach( int n, Bar bar; foo.arr ) writefln("A=%s", bar.a );
}


-- 
Jul 13 2006
next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=251






use inout

  with(foo)
  {
    foreach( int n, inout Bar bar; arr ) bar.a = 100;
  }

haven't tested this but...

INVALID I think


-- 
Jul 13 2006
parent reply David Medlock <noone nowhere.com> writes:
d-bugmail puremagic.com wrote:

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

 use inout
 
   with(foo)
   {
     foreach( int n, inout Bar bar; arr ) bar.a = 100;
   }
 
 haven't tested this but...
 
 INVALID I think
 
 
Has this changed? I know the opApply used to require inout arguments, and they were implied mutable on iteration.
Jul 13 2006
parent reply BCS <BCS pathlink.com> writes:
David Medlock wrote:
 d-bugmail puremagic.com wrote:
 
 http://d.puremagic.com/issues/show_bug.cgi?id=251






 use inout

   with(foo)
   {
     foreach( int n, inout Bar bar; arr ) bar.a = 100;
   }

 haven't tested this but...

 INVALID I think
Has this changed? I know the opApply used to require inout arguments, and they were implied mutable on iteration.
I think that a basic foreach on an array requiters inout to change things. http://www.digitalmars.com/d/statement.html#foreach The opApply does requirer the inout (I ran into this a week or so back). This seems like a problem to me. A non-inout version should be allowed so that read only access can be granted.
Jul 13 2006
next sibling parent David Medlock <noone nowhere.com> writes:
BCS wrote:
 David Medlock wrote:
 
 d-bugmail puremagic.com wrote:

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






 use inout

   with(foo)
   {
     foreach( int n, inout Bar bar; arr ) bar.a = 100;
   }

 haven't tested this but...

 INVALID I think
Has this changed? I know the opApply used to require inout arguments, and they were implied mutable on iteration.
I think that a basic foreach on an array requiters inout to change things. http://www.digitalmars.com/d/statement.html#foreach The opApply does requirer the inout (I ran into this a week or so back). This seems like a problem to me. A non-inout version should be allowed so that read only access can be granted.
Agreed.
Jul 13 2006
prev sibling parent reply "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
BCS wrote:

 I think that a basic foreach on an array requiters inout to change things.
 
 http://www.digitalmars.com/d/statement.html#foreach
 
 The opApply does requirer the inout (I ran into this a week or so back). This
 seems like a problem to me. A non-inout version should be allowed so that
 read only access can be granted.
I agree too. But I would also like to have a warning or even completely disallow modifications of variables in foreach that are not declared as inout. The present situation is too error-prone: ----------------------------------------------------- foreach(i,k; a) // just forgot to type "inout". code does nothing k=i+1; ----------------------------------------------------- should be ----------------------------------------------------- foreach(i,k; a) k=i+1; // "warning: k is not inout" or "error: k is immutable". foreach(i,inout k; a) // ok k=i+1; ----------------------------------------------------- or maybe just a simple solution is to make them inout by default? --
Jul 14 2006
parent BCS <BCS pathlink.com> writes:
Andrei Khropov wrote:
 
 But I would also like to have a warning or even completely disallow
 modifications of variables in foreach that are not declared as inout.
 The present situation is too error-prone:
 
[...]
 
 or maybe just a simple solution is to make them inout by default?
 
No!!! An error/warning would be enough.
Jul 14 2006
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=251


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID





This has nothing to do with it being in a with statement.

At one point, the 'key' part of the foreach was set to 'final', meaning it
could not be reassigned. This fell afoul of all the tail const problems, so it
was abandoned.

The current compiler is working as designed. The 'key' value is a mutable copy,
unless it is declared as 'inout'.


-- 
Jun 24 2008