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.
Official Announcement: 'Learning D' is Released, a new book!
I haven't read the whole book yet, but from the sample and author Mike Parker's other work, I'm pretty confident in expecting quality from the book!
TWID Readers: if you want to write a review for any of our D books and email it to me, firstname.lastname@example.org, I'll add them here too!
Release D 2.069.2 was also made this week with bug fixes.
See more at the announce forum.
D has an excellent feature called bounds checking. Arrays and slices know their own length, so trying to access an index before or after the valid area of the array will throw a RangeError, protecting you from memory corruption problems.
Bounds checking is useful for safety, correctness, and debugging, but in tight loops, it can be a performance hit. As such, the compiler has an option to disable it: -boundscheck=[on|safeonly|off] bounds checks on, in @safe only, or off.
With this, combined with separate compilation of modules or packages, you can turn it off in sections of the program, but leave it on for others. With -boundscheck=safeonly, you can disable it in tight @system code, while keeping it in @safe code, but still you may want finer-grained control, and that's where you need to get a little creative.
The trick is to use a pointer! Pointers in D can still be indexed (as in C), but do not know their length, and thus do not do bounds checks. This is considered @system, since you are taking memory-safety into your own hands, but that's true whenever you disable bounds checking.
The generated code for arr.ptr is the same as arr, with the exception of skipping the bounds check. There is no cost in accessing the .ptr property of a D array/slice.
So, if you need to disable access checking in a single expression rather than a whole module, you don't want the command line switch, instead, just use the .ptr property.
To learn more about D and what's happening in D: