[Issue 1698] New: foreach auto type inference doesnt work properly

Sönke Ludwig ludwig at informatik_dot_uni-luebeck.de
Mon Dec 3 05:45:51 PST 2007


Janice Caron schrieb:
> On 12/1/07, Sönke Ludwig <ludwig at informatik_dot_uni-luebeck.de> wrote:
>> Since c inside of the foreach-loop is actually a "ref invariant(char)"
>> (string = invariant(char)[]), it is correct that it cannot be passed to
>> bar, taking a mutable "ref char".
>> Changing bar to "void bar( ref invariant(char) c ){}" makes it compile
>> again.
> 
> So... can you /assign/ a ref invariant(char)?
> 
> I'm confused as to why the code isn't just
> 
>     foreach(c;text)
> 

You surprisingly can.. actually you can do:

import std.stdio;
void main()
{
	string text = "Hello, World!".idup;
	
	foreach( ref c; text )
		c = 'X';

	writefln(text); // prints "XXXX...
}

But you cannot do "text[0] = 'X';". So it seems to me arrays with the 
new const system are actually quite broken for now.


More information about the Digitalmars-d-bugs mailing list