A famous saying in the software development world is that “programmers should learn a new language every year”. I have not really done that in quite a while, for a multitude of reasons: First of all, I don’t think that one really learns a programming language within a year. Yes, it is easy to get started and be able to read and write simple programs, but it usually takes quite a bit longer until one really absorbs the finer details of a language. Secondly, there is hardly any use in knowing more than 4 or 5 major languages and I’m doing pretty well with fluently writing C/C++ and Python code (though, of course, I can read quite a few more programming languages like, for example, Java and good old ASM). That being said, there is always a new technology rolling around and a lot of times it is worthwhile to take at least a brief look at these just in case major parts of the industry start jumping on it (remember: if you are not part of the steamroller, you are likely part of the road). And sometimes you really just want to see if there is a replacement for the horribly broken stuff that you have to deal with on a daily base at work (yes, I am looking at you, C++).

One specific language that has been popping up on my radar every now and then over the last 2-3 years was the D programming language. It claims to be a systems programming language and to have the same efficiency as C/C++ while coming with all the bells and whistles that “modern” languages bring along. Furthermore I’ve been a big fan of Andrei Alexandrescu ever since I read his book “Modern C++ Design”, and since he has a major involvement in the development of D and happens to be the author of “The D Programming Language” book, it is more than fair to say that it is time to finally sit down and give this language a serious chance.

As with every programming language, you first need a compiler (or interpreter) that will take care of translating source code into instructions the computer understands. To my surprise there are already different D compilers available: GDC (based on GCC), LDC (based on LLVM) and DMD (the reference implementation). For the purpose of learning I’ve decided to go with DMD.

Once I was done installing the compiler I fired up a command line window and navigated to the samples directory. A quick glance at the file listing reveals that there is a file called hello.d, and after opening it in VIM it turns out to be the traditional “Hello World” program, as expected:

import std.stdio;

void main(string[] args)
{
  writeln("hello world");
  writefln("args.length = %d", args.length);

  foreach (index, arg; args)
  {
    writefln("args[%d] = '%s'", index, arg);
  }
}

If you now say “Hang on a second, that looks a lot like a mix of Python and C!” then I’m fully in agreement, as that was pretty much my first response as well. To begin with, the first line is an import statement like we know it from Python, except it’s referencing something that is named similar to the C/C++ standard libraries (C++’s standard library uses a namespace called std, while C has a standard library file stdio).

Next up there is a function declaration that looks very similar to the main function as we know it from C - except it only takes one argument args (an array of strings) as opposed to the classical two arguments argc (an integer specifying the number of passed arguments) and argv (a pointer to char* containing a list of argc arguments). A quick peek at the contents of the function (and a swift look at the language documentation) reveal that argc is obsolete because arrays are a built-in type in D - very nice, even if it’s not particularly new. Then there’s the writefln() function which appears to be very similar to C’s printf() / Python’s print(). And last but not least we see another interesting construct in this example: The foreach loop. Now, this kind of loop is nothing new either (for example it exists in Python as for x in sequence). However, in D it comes with a special syntax for arrays where you can declare two variables instead of one, meaning that the first variable will be the index (or key in the case of an associative array) of the element you are iterating over and the second variable the element. Another big convenience right away, as it obsoletes managing a separate counter variable which could be a potential source of errors when facing a more complicated loop logic. And remember: strings are just an array of character…

But enough about the code for now. Let’s go and get it to run. There are shell- / batch-scripts which compile all examples, but I am naive and only want the hello world example. Thus I just type dmd hello.d into the terminal and see what happens… interesting, no error and the CPU seems to be busy. But then nothing happened apparently, I just see a new line in the terminal to enter the next command. A new directory listing reveals that there is now an executable called hello.exe. Executing that works just fine and prints out “hello world” among the length and contents of the argument list the main function received. A quick look at dmd –help then also reveals that I wouldn’t have had to compile the code: invoking dmd with the -run argument would have simply executed the D code like a script. This is another surprise as that means that one could theoretically use D to write small utility scripts. And -run is not the only fascinating command line switch for dmd: it also contains the -unittest option to compile in unittests (which are built into the language) and a -D switch which seems to generate documentation from the source code (yup, documentation is also built into the language). Well then, on to write a first small piece of software in D myself, but more about that in the next post - right now it’s time to enjoy another glorious and beautiful Icelandic sunset.

PS: I know that I owe you the final part of the software engineering exercise article series. It is sitting here as a draft but I’m not quite happy with its wording just yet, so it will take a bit longer until it is done.