Newbie problem
Adam D. Ruppe
destructionator at gmail.com
Wed Jun 19 08:38:39 PDT 2013
On Wednesday, 19 June 2013 at 15:25:15 UTC, Roger Stokes wrote:
> page406x.d(11): Error: cannot implicitly convert expression
> (__r24.front()) of type ubyte[] to immutable(ubyte)[]
> foreach (immutable(ubyte)[] buffer;
> stdin.byChunk(bufferSize)) {
The problem here is that byChunk returns a mutable buffer, type
ubyte[]. If you said "foreach(ubyte[] buffer;
stdin.byChunk(bufferSize)) {...}" you should be able to compile
it.
I think this was changed after the book was written because
byChunk now reuses its buffer. Each time through the loop, it
overwrites the old data with the next batch, which means the old
data cannot be immutable. (The reason for reusing it is to avoid
allocating new memory for a fresh buffer every time.)
Since you aren't keeping a copy of the buffer, just changing the
type should be enough for your program to work.
If you needed an immutable copy, if you were going to store it or
something, the way you'd do that is to call buffer.idup:
foreach(ubyte[] temporaryBuffer; stdin.byChunk(bufferSize) {
immutable(ubyte)[] permanentBuffer = temporaryBuffer.idup;
// you can now use the permanentBuffer
}
More information about the Digitalmars-d-learn
mailing list