digitalmars.D.bugs - [Issue 251] New: foreach does not allow updating inside with block
- d-bugmail puremagic.com Jul 13 2006
- d-bugmail puremagic.com Jul 13 2006
- David Medlock <noone nowhere.com> Jul 13 2006
- BCS <BCS pathlink.com> Jul 13 2006
- David Medlock <noone nowhere.com> Jul 13 2006
- "Andrei Khropov" <andkhropov nospam_mtu-net.ru> Jul 14 2006
- BCS <BCS pathlink.com> Jul 14 2006
- d-bugmail puremagic.com Jun 24 2008
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 ------- Comment #1 from shro8822 uidaho.edu 2006-07-13 18:46 ------- 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 ------- Comment #1 from shro8822 uidaho.edu 2006-07-13 18:46 ------- use inout with(foo) { foreach( int n, inout Bar bar; arr ) bar.a = 100; } haven't tested this but... INVALID I think
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:http://d.puremagic.com/issues/show_bug.cgi?id=251 ------- Comment #1 from shro8822 uidaho.edu 2006-07-13 18:46 ------- use inout with(foo) { foreach( int n, inout Bar bar; arr ) bar.a = 100; } haven't tested this but... INVALID I think
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
BCS wrote:David Medlock wrote:d-bugmail puremagic.com wrote:http://d.puremagic.com/issues/show_bug.cgi?id=251 ------- Comment #1 from shro8822 uidaho.edu 2006-07-13 18:46 ------- use inout with(foo) { foreach( int n, inout Bar bar; arr ) bar.a = 100; } haven't tested this but... INVALID I think
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
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 ------- Comment #3 from bugzilla digitalmars.com 2008-06-24 21:53 ------- 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> 