This Week in D November 27, 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.


In the community

Community announcements

See more at the announce forum.

Tip of the Week

D structs have an initializer, even if they are used for interfacing with C. This is a small blob of data called something like _Dxxstruct_name__initZ, or can be all zeroes, and is generated with the interface module.

So if you use the initializer from the interface module, but do not link it in, you get an undefined symbol.

Easiest fix: just link in that .d file and use the automatic features.

Why does your struct have an initializer data symbol instead of being all zeroes? I'm not entirely sure, but I think it is because the contents are structs and it didn't look inside those structs to see if they too are zeroes, it just assumed struct = fancy initialized. That's arguably an enhancement request, but I wouldn't call it a bug.

You can often bypass the initializer by using =0 or =void in definitions, but not here... and that is arguably a bug, if you =void all fields, it should be free to zero initialize the whole thing, but it doesn't.

When do you use the initializer? Any time an item is default initialized: struct variable definitions, static arrays, or dynamic array appends. When nothing is default initialized, you don't use it, such as pointers and dynamic arrays which start as null (and thus point to no actual struct data). If you are explicitly initializing it, no default needed.

You can bypass defalt initialization this with =void at the end of that variable definition.

Dynamic array length increases and appends also default initializes the new items you add. Just don't use that length function if you don't want to link in the other .d file.

But while the associated linker errors can be annoying, it isn't really a bug - it is D's auto initialize feature creating some data that you didn't link. Avoiding D-specific features and bypassing initialization with =void can skip it, but I recommend just linking in the module.

Learn more about D

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