www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - foreach and inout

reply "Valéry Croizier" <valery freesurf.fr> writes:
Consider :

class A { int x;}
A[10] t;
foreach(A a; t) // should be "A inout a"
    a = new A;

Later, an access to any element of 't' is likely to segfault (happened to me
:).
The compiler should raise an error.
Nov 23 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <co0cg2$24hq$1 digitaldaemon.com>, Valéry Croizier says...
Consider :

class A { int x;}
A[10] t;
foreach(A a; t) // should be "A inout a"
    a = new A;

Later, an access to any element of 't' is likely to segfault (happened to me
:).
The compiler should raise an error.
This is completely legal from a syntactical perspective--D doesn't have any concept of logical const-ness. All you're doing is assigning a new value to a variable. Sean
Nov 23 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Tue, 23 Nov 2004 22:49:06 +0000 (UTC), Sean Kelly <sean f4.ca> wrote:

 In article <co0cg2$24hq$1 digitaldaemon.com>, Valéry Croizier says...
 Consider :

 class A { int x;}
 A[10] t;
 foreach(A a; t) // should be "A inout a"
    a = new A;

 Later, an access to any element of 't' is likely to segfault (happened 
 to me
 :).
 The compiler should raise an error.
This is completely legal from a syntactical perspective--D doesn't have any concept of logical const-ness. All you're doing is assigning a new value to a variable.
Correct, if however my earlier suggestion that modifying an 'in' variable should be illegal was used this would have given an error. This is the 3rd or 4th instance where the idea would have prevented a bug. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 23 2004