Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 13, 2006 [Issue 251] New: foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
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 ); } -- |
July 13, 2006 [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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 -- |
July 13, 2006 Re: [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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
>
>
Has this changed?
I know the opApply used to require inout arguments, and they were implied mutable on iteration.
|
July 14, 2006 Re: [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | 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 >> >> > 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. |
July 14, 2006 Re: [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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
>>>
>>>
>> 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.
|
July 14, 2006 Re: [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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? -- |
July 14, 2006 Re: [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Khropov | 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. |
June 25, 2008 [Issue 251] foreach does not allow updating inside with block | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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'. -- |
Copyright © 1999-2021 by the D Language Foundation