This Week in D April 24, 2016

Welcome to This Week in D! Each week, we'll summarize what's been going on in the D community and write brief advice columns to help you get the most out of the D Programming Language.

The D Programming Language is a general purpose programming language that offers modern convenience, modeling power, and native efficiency with a familiar C-style syntax.

This Week in D has an RSS feed.

This Week in D is edited by Adam D. Ruppe. Contact me with any questions, comments, or contributions.

Statistics

Major Changes

A patch was merged this week that allows subdirectories in string imports (import("dir/file");) on Windows. It was already allowed on Linux, but prohibited on Windows. Now forward slashes are permitted on both platforms. The subdirectories must still be under the path passed with the -J option to dmd.

subdirectories in windows

Google Summer of Code

Google Summer of Code selections were announced this week. D got four slots and work will progress on a std.xml replacement, dstep improvements, a precise GC with an aim for future enhancements, and a new random number generator for the D science libraries project.

DConf 2016

Live streaming of DConf 2016 is confirmed. The entire conference will be livestreamed and recorded.

In the community

Community announcements

See more at the announce forum.

Tip of the Week

std.stdio is somewhat infamous for reusing its buffer: a significant potential performance enhancement (and definitely a right choice to have available, but not necessarily a good choice for the default, considering how many new users trip up on this), but also something users need to be aware of in order to .dup the buffer when needed.

Well, std.stdio is not the only place where mutable buffers cause a pitfall! std.zlib does not overwrite a buffer you pass it... but it does keep a reference to it, meaning YOU must not overwrite the buffer, or you will be liable to get an exception with the message "Data error".

The following code is what you might want to write, but it is wrong:

	import std.stdio, std.zlib;
	auto uncompress = new Uncompress();
	ubyte[] result;
	foreach(chunk; stdin.byChunk)
		result ~= uncompress.uncompress(chunk); // this is wrong!
	result ~= uncompress.flush();

It will work many times, but not every time. Since uncompress keeps a pointer to the chunk (zlib may need more data than is available in the chunk, so uncompress will hold the pointer to the existing chunk, expecting that it can append data from the next chunk to the existing chunk), overwriting the buffer in between loop iterations will corrupt the data...

And since byChunk will reuse its buffer, this seemingly elegant code is quite incorrect.

The problem can be fixed by simply adding .dup in the call to uncompress.

You may also want to just use the lower-level C functions yourself for maximum control and to avoid excess memory allocation and copying, or the top-level uncompress instead of the classes - preloading everything at once may also be more efficient in some cases.

But the general rule is to be careful with mutable buffers and slices! Library code may save it without informing you.

std.zlib and byChunk fail because byChunk modifies the buffer and std.zlip actually holds on to a slice of it!!!!

Learn more about D

To learn more about D and what's happening in D: