D (programming language)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
D programming language
Paradigm multi-paradigm: procedural, object-oriented, functional, generic
Designed by Walter Bright, Andrei Alexandrescu (since 2006)
Developer Digital Mars, Andrei Alexandrescu (since 2006)
First appeared 2001 (2001)[1]
Stable release 2.071.1[2] / June 27, 2016; 16 months ago (2016-06-27)[3]
Typing discipline strong, static
OS DMD: Unix-like, Windows, Mac OS X
License GPL/Artistic (DMD frontend),
Boost (standard and runtime libraries),
source available (DMD backend),[4]
Fully open-source (LDC and GDC)[5]
Filename extensions .d
Website dlang.org
Major implementations
DMD (reference implementation), GDC, LDC
Influenced by
C, C++, C#, Eiffel, Java, Python, Ruby
MiniD, DScript, Vala, Qore

The D programming language is an object-oriented, imperative, multi-paradigm system programming language. D language originated as a re-engineering of C++, and D's design goals try combining the performance of compiled languages with the safety and expressive power of modern dynamic languages. Native D code is commonly as fast as equivalent C++ code, while being shorter and memory-safe.[6]

Examples[change | change source]

Example 1[change | change source]

This example program prints its command line arguments. The main function is the entry point of a D program, and args is an array of strings representing the command line arguments. A string in D is an array of characters, represented by char[] in D1, or immutable(char)[] in D2.

import std.stdio: writefln;

void main(string[] args)
    foreach (i, arg; args)
        writefln("args[%d] = '%s'", i, arg);

The foreach statement can iterate over any collection. In this case, it is producing a sequence of indexes (i) and values (arg) from the array args. The index i and the value arg have their types inferred from the type of the array args.

Example 2[change | change source]

The following shows several D capabilities and D design trade-offs in a very short program. It iterates the lines of a text file named words.txt that contains a different word on each line, and prints all the words that are anagrams of other words.

import std.stdio, std.algorithm, std.range, std.string;

void main()
    dstring[][dstring] signs2words;
    foreach(dchar[] w; lines(File("words.txt")))
        w = w.chomp().toLower();
        immutable key = w.dup.sort().release().idup;
        signs2words[key] ~= w.idup;

    foreach(words; signs2words)
        if(words.length > 1)
            writefln(words.join(" "));
  1. signs2words is a built-in associative array that maps dstring (32-bit / char) keys to arrays of dstrings. It is similar to defaultdict(list) in Python.
  2. lines(File()) yields lines lazily, with the newline. It has to then be copied with idup to obtain a string to be used for the associative array values (the idup property of arrays returns an immutable duplicate of the array, which is required since the dstring type is actually immutable(dchar)[]). Built-in associative arrays require immutable keys.
  3. The ~= operator appends a new dstring to the values of the associate dynamic array.
  4. toLower, join and chomp are string functions that D allows to use with a method syntax. The name of such functions is often very similar to Python string methods. The toLower converts a string to lower case, join(" ") joins an array of strings into a single string using a single space as separator, and chomp removes a newline from the end of the string if one is present.
  5. The sort is an std.algorithm function that sorts the array in place, creating a unique signature for words that are anagrams of each other. The release() method on the return value of sort() is handy to keep the code as a single expression.
  6. The second foreach iterates on the values of the associative array, it's able to infer the type of words.
  7. key is assigned to an immutable variable, its type is inferred.
  8. UTF-32 dchar[] is used instead of normal UTF-8 char[] otherwise sort() refuses to sort it. There are more efficient ways to write this program, that use just UTF-8.

References[change | change source]

  1. "D Change Log to Nov 7 2005". D Programming Language 1.0. Digital Mars. Retrieved 13 June 2014. 
  2. "Change Log: List of All Versions". D Programming Language 2.0. Digital Mars. Retrieved 30 September 2016. 
  3. "Change Log: 2.071.1". D Programming Language 2.0. Digital Mars. Retrieved 30 September 2016. 
  4. "readme.txt". DMD source code. GitHub. Retrieved 13 June 2014. 
  5. FAQ of digitalmars Retrieved 13 June 2014
  6. Bright, Walter. D programming Language Specification (e-book ed.). 7227: Digital Mars (via Amazon). 

Further reading[change | change source]

  • Alexandrescu, Andrei (January 4, 2010). The D Programming Language (1 ed.). Addison-Wesley Professional. ISBN 978-0-321-63536-5. 
  • Alexandrescu, Andrei (June 15, 2009). "The Case for D". Dr. Dobb's Journal. 
  • Çehreli, Ali (February 1, 2012). "Programming in D".  (distributed under CC-BY-NC-SA license). This book provides a basic level introduction.

Other websites[change | change source]