This Week in D January 18, 2015

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 is the first edition of This Week in D and we're looking for help and feedback. Any suggestions for style, visual design, or content are welcome!

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

Statistics

Major Changes

Project Spotlight

The spotlight this week is on this publication, This Week in D.

This Week in D is written in Ddoc and is about all things D, with the blessing of Walter Bright and Andrei Alexandrescu. It is a new project, and thus may still be a little rough around the edges - contributions are always welcome.

It wants to be more than just a collection of statistics and links. This Week in D should be an interesting read for casual observers and experienced D users alike, with a weekly tip column and project spotlight from me or the community to help get the word out about what you're working in on D.

This Week in D keeps its reader appraised about what's going on in D's whole ecosystem!

Want your project featured here? Prepare a link and write up a few paragraphs about it, then contact me! Your project does not have to be complete to be featured - we like works-in-progress too.

In the community

DConf 2015 announced!

DConf 2015 will be held at Utah Valley University in Orem, Utah, from May 27, 2015 through May 29, 2015!

UVU is sponsoring DConf 2015 and the conference organizers are looking for talk submissions now.

Hope to see you there!

Community announcements

See more at digitalmars.D.announce.

Significant Forum Discussions

See more at forum.dlang.org and keep up with community blogs at Planet D.

Tip of the week

This week, we'll look at how to use a custom class allocator at the call site.

Class objects in D are typically allocated with the new operator:

	// classes are reference types, so they must be
	// initialized. "MyClass c;", unlike in C++, would
	// leave c as null, causing a segfault when you try
	// to use it.
	//
	// The normal way to initialize a class is with new:
	MyClass c = new MyClass(args...);

There are times when you won't want to do that, however, such as when you are avoiding the garbage collector or wanting to use a custom allocator.

You can also avoid the garbage collector in your critical loop by pre-allocating your objects before entering the main loop of your program, then reusing those objects as needed.

The alternative to new is to do it yourself: allocate the memory yourself, then call std.conv.emplace to perform a placement new in that memory. You can allocate the memory with malloc for C style heap memory, or as a static array to place it on the stack:

	import std.conv : emplace;

	ubyte[__traits(classInstanceSize, YourClass)] buffer;

	YourClass obj = emplace!YourClass(buffer[], ctor args...);
	// Destroy the object explicitly at scope exit, which will
	// call the destructor deterministically.
	scope(exit) .destroy(obj);

	// use obj normally here

The __traits(classInstanceSize, YourClass) portion is important: it tells you the size of a class object. YourClass.sizeof won't work because classes are reference types. It will return the size of the underlying pointer instead of the object itself!

Once your memory is allocated, you can place the class there then proceed to use it normally. The static array and scope guard will imitate C++'s class behavior in D - allocated on the stack and destroyed when it goes out of scope.

Wrapping class references in structs for automatic management is possible, but difficult to get correct when faced with inheritance. Thus, you'll probably use regular class references. When passing them around, be sure not to store those references improperly. Without the garbage collector, you're back in the world of manual memory management, which means you are responsible to get it right yourself!

Want to learn more about the library function? Check out the emplace source code.


Find more D tips at the D idioms list or buy my D Cookbook for a more in-depth examination many D tricks.

If you'd like to submit a tip, email me.

New people and projects

Would you like to introduce yourself or your project? Let me know! Post to the D announcements forum or email me privately.

Upcoming events

Learn more about D

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