digitalmars.D.bugs - [Issue 251] New: foreach does not allow updating inside with block
- d-bugmail puremagic.com (32/32) Jul 13 2006 http://d.puremagic.com/issues/show_bug.cgi?id=251
- d-bugmail puremagic.com (10/10) Jul 13 2006 http://d.puremagic.com/issues/show_bug.cgi?id=251
- David Medlock (4/23) Jul 13 2006 Has this changed?
- BCS (6/31) Jul 13 2006 I think that a basic foreach on an array requiters inout to change thing...
- David Medlock (2/38) Jul 13 2006 Agreed.
- Andrei Khropov (18/25) Jul 14 2006 I agree too.
- BCS (3/11) Jul 14 2006 No!!! An error/warning would be enough.
- d-bugmail puremagic.com (14/14) Jun 24 2008 http://d.puremagic.com/issues/show_bug.cgi?id=251
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
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
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
David Medlock wrote:d-bugmail puremagic.com 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.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 thinkHas this changed? I know the opApply used to require inout arguments, and they were implied mutable on iteration.
Jul 13 2006
BCS wrote:David Medlock wrote:Agreed.d-bugmail puremagic.com 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.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 thinkHas this changed? I know the opApply used to require inout arguments, and they were implied mutable on iteration.
Jul 13 2006
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
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
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









David Medlock <noone nowhere.com> 