This Week in D March 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.

Statistics

Major Changes

Beta D 2.071.0-b1 was released this week, test it on your larger codebases!

In the development branch, the volatile keyword, which has been an error to use for a very long time now, was excised from the language.

Further work has been done to clean up the import bugs, along with a transition switch in git head to aid fixing broken code to be up to the new, higher standard.

In the community

Community announcements

See more at the announce forum.

Tip of the Week

A lot of people ask me if there's a way to get a stack trace while running. This is an updated version of the code from the appendix in my book to show just the most relevant lines. It returns a string which you can print out using your favorite functions.

string getStackTrace() {
  import core.runtime;

  version(Posix) {
    // druntime cuts out the first few functions on the trace because they are internal
    // so we'll make some dummy functions here so our actual info doesn't get cut
    Throwable.TraceInfo f2() { return defaultTraceHandler(); }
    Throwable.TraceInfo f1() { return f2(); }
    auto trace = f1();
  } else {
    auto trace = defaultTraceHandler();
  }

  // cut off the last 7 lines because they are internal
  // functions anyway
  import std.string;
  return join(splitLines(trace.toString())[0 .. $-7], "\n");
}

Since it slices the string according to some internal druntime implementation details, it will need attention now and then, but the core mechanism - a call to defaultTraceHandler - ought to be well-supported (the Exception classes use it), so you might just need to tweak the slicing every now and then to keep it working beautifully.

Paste that function into code you are trying to writeln debug and get stack traces whenever you need them! Compile with -g to turn on debug data to get file and line numbers in the stack trace.

Of course, you can also run your program in an interactive debugger and break to get a stack trace there. In gdb for example, pressing ctrl+c or setting a break point will stop your program, then the where command will tell you where you are via stack trace info.

Learn more about D

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