www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Stream.opApply and invariance

reply Michael Coupland <mcoupland gmail.com> writes:
The following example from 
http://www.digitalmars.com/d/2.0/phobos/std_stream.html does not seem to 
compile with the latest D compiler:

	Stream file = new BufferedFile("sample.txt");
	foreach(ulong n, string line; file) {
	  stdout.writefln("line %d: %s",n,line);
	}
	file.close();

The error is due to the D 2.0 string/invariance changes (which I'm still 
trying to wrap my head around; is there a good reference that sorts it 
all out?) [*]

The most literal, valid translation of the above code that I can manage 
is as follows:

	Stream file = new BufferedFile("sample.txt");
	foreach(ulong n, char[] char_ar; file)
	{
		string line = char_ar.idup;
		writefln("line %d: %s",n,line);
	}
	file.close();

although for this particular example, you could just replace string with 
char[]. The problem with that approach is that you usually want a string 
at some point, so you can do all the wonderful string operations that 
one tends to do.

Is this just a case where the Stream library just hasn't been 
string-invariance-ized yet? Or is there a different way to do things 
now? (In which case I recommend updating the documentation!)

	Michael







[*] It's worth noting that there's also an error with 'stdout.writefln' 
hence my replacement with 'writefln' in the second block of code.
Apr 04 2008
parent Lionello Lunesu <lio lunesu.remove.com> writes:
This one bugs me as well. The reason is that opApply uses readLine, 
which in turn calls readLine(char[]), reusing the passed buffer. This 
will never return an invariant string, and the result must therefor 
always be idup'ed :-((

I don't think there's anything that can be done to 'fix' this. readLine 
must write to the buffer, so it's never invariant. Casting to invariant 
is not possible, as it would break all the guarantees. I wonder if 
there's something that can be done by using an "invariant constructor" 
mentioned in Andrei's PDF (see Walter's post in the other group.)

L.
Apr 05 2008