[Issue 251] foreach does not allow updating inside with block

Andrei Khropov andkhropov at nospam_mtu-net.ru
Fri Jul 14 07:35:16 PDT 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?

-- 




More information about the Digitalmars-d-bugs mailing list