D (programming language)

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.

### Example 1

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.

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

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.

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.
1. `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.
1. The `~=` operator appends a new dstring to the values of the associate dynamic array.
1. `toLower`, `join` and `chomp` are string functions that may be used with a method-like 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.
1. 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.
1. The second `foreach` iterates on the values of the associative array, it's able to infer the type of `words`.
1. `key` is assigned to an immutable variable; its type is inferred.
1. 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.

## Further reading

- (distributed under CC-BY-NC-SA license). This book provides a basic level introduction.

## Infobox (programming language)

- **name**: D programming language
- **file ext**: `.d`
- **paradigm**: multi-paradigm programming language, multi-paradigm: procedural programming, procedural, object-oriented programming, object-oriented, functional programming, functional, generic_programming, generic
- **designer**: Walter Bright, Andrei Alexandrescu (since 2006)
- **developer**: Digital Mars, Andrei Alexandrescu (since 2006)
- **typing**: strong typing, strong, static typing, static
- **implementations**: DMD (reference implementation), GDC, LDC
- **influenced by**: C (programming language), C, C++, C Sharp (programming language), C#, Eiffel (programming language), Eiffel, Java (programming language), Java, Python (programming language), Python, Ruby (programming language), Ruby
- **influenced**: MiniD, DScript, Vala (programming language), Vala, Qore Language, Qore
- **operating system**: DMD: Unix-like, Microsoft Windows, Windows, Mac OS X
- **license**: GNU General Public License, GPL/Artistic License, Artistic (DMD frontend),
Boost Software License, Boost (standard and runtime libraries),
source available (DMD backend),
Fully open-source (LDC and GDC)
- **wikibooks**: D Programming