D

Appeared in:
2001
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.d
Versions and implementations (Collapse all | Expand all):
Programming language

The D programming language is a compiled multi-paradigm language developed by Walter Bright at Digital Mars since 1999.

A very short list of what D offers:

  1. All the type-casting and pointer manipulation necessary for low level systems programming.
  2. Built-in garbage collection with the ability to turn it off for low level work.
  3. Standard structuring elements.
  4. A full set object orienting elements including abstraction, encapsulation, single inheritance, and polymorphism.
  5. Generic programming.

There are two standard libraries: Phobos (more official) and Tango (more of an “all objects all the time” style). They are not compatible, but work is progressing to make them compatible.

The first version of the language D1 supported imperative, object-oriented and metaprogramming paradigms, similarly to C++. The second version D2 was being developed since 2007; it focused on adding new paradigms (functional and concurrent) as well as some modern features. It is considered to be stable since 2010, when a K&R type book for D, “The D Programming Language” by Andrei Alexandrescu, was published.

A shorter book — “Learn to Tango with D” by Kris Bell, Lars Ivar Igesund, Sean Kelly, and Michael Parker — was published in 2008. It covers D1 only and uses the Tango library.

Elements of syntax:

Inline comments //
Nestable comments /+ ... +/
Non-nestable comments /* ... */
Case-sensitivity Yes
Variable identifier regexp [_a-zA-Z][_a-zA-Z0-9]*
Function identifier regexp [_a-zA-Z][_a-zA-Z0-9]*
Variable assignment <varname> = <value>
Variable declaration <type> <varname>
Variable declaration with assignment <type> <varname> = <value>
Grouping expressions ( ... )
Block { ... }
Physical (shallow) equality x is y
Physical (shallow) inequality x !is y
Deep equality x == y
Deep inequality x != y
Comparison > >= < <=
Function definition type fn(type1 arg1, ... typeN argN)
Function call fn(arg1, ..., argN)
Function call with no parameters fn()
Sequence ;
If - then if (condition) ...
If - then - else if (condition) ... else ...
Loop forever while (true) ...
While condition do while (condtion) ...
Do until condition do ... while (condtion)
For each value in a numeric range, 1 increment for (int i = first; i < limit ; i++) ...
For each value in a numeric range, 1 decrement for (int i = limit - 1; i >= first; i--) ...

Examples:

Hello, World!:

Example for versions D1, D2, gdc 0.24

writef() and writefln() writes to standard output and interpret the first argument as a format string. They are roughly analogous to C’s printf(). writefln() automatically appends a newline. D2 adds write() and writeln() which do not interpret the first argument as a format string. However, they are not available in D1.

module hello;

import std.stdio;

int main()
{
	writefln( "Hello, World!" );
	return 0;
}

Factorial:

Example for versions D1, D2, gdc 0.24

This example uses recursive factorial definition.

module factorial;

import std.stdio;

ulong recursive(ulong x)
{
	return (x == 0 ? 1 : x * recursive( x - 1 ));
}

int main()
{
	for (int i = 0; i < 17; ++i)
	{
		writefln("%s! = %s", i, recursive(i));
	}
	return 0;
}

Fibonacci numbers:

Example for versions D1, D2, gdc 0.24

This example uses iterative definition of Fibonacci numbers.

module fibonacci;

import std.stdio;

ulong iterative(ulong x)
{
    ulong prev1 = 1L;
    ulong prev2 = 1L;
    ulong result = x <= 2 ? 1L : 0L;
    
    for ( ulong i = 3; i <= x; ++i )
    {
        result = prev1 + prev2;
        prev1 = prev2;
        prev2 = result;
    }
    return result;

}

int main()
{
	for (uint i = 1; i < 17; i++)
	{
		writef("%s, ", iterative(i));
	}
	writefln("%s", "...");
	
	return 0;
}

Fibonacci numbers:

Example for versions D1, D2, gdc 0.24

This example uses recursive definition of Fibonacci numbers.

module fibonacci;

import std.stdio;

ulong recursive(ulong x)
{
	return x <= 2 ? 1 : recursive( x - 2 ) + recursive( x - 1 );
}

int main()
{
	for (uint i = 1; i < 17; i++)
	{
		writef("%s, ", recursive(i));
	}
	writefln("%s", "...");
	
	return 0;
}

Factorial:

Example for versions D1, D2, gdc 0.24

This example uses iterative factorial definition. Note the usage of foreach loop.

module factorial;

import std.stdio;

ulong iterative(ulong x) {
	ulong result = 1;
	foreach (ulong count; 1..x+1)
		result *= count;
	return result;
}

int main() {
	foreach (int i; 0..17)
		writefln("%s! = %s", i, iterative(i));
	return 0;
}

Quadratic equation:

Example for versions D1, D2
import std.c.stdio;
import std.stdio;
import std.math;

int main() {
    int A, B, C;
    writef("A = ");
    scanf("%d", & A);
    if (A==0)
    {   writefln("Not a quadratic equation.");
        return 0;
    }
    writef("B = ");
    scanf("%d", & B);
    writef("C = ");
    scanf("%d", & C);
    A*=2;
    float D = B*B-2*A*C;
    if (D == 0)
    {   writefln("x = %f\n",-B*1.0/A);
        return 0;
    }
    if (D>0)
        writefln("x1 = %f\nx2 = %f",(-B+sqrt(D))/A,(-B-sqrt(D))/A);
    else    
        writefln("x1 = (%f, %f)\nx2 = (%f, %f)",-B*1.0/A,sqrt(-D)/A,-B*1.0/A,-sqrt(-D)/A);
    return 0;
}

CamelCase:

Example for versions D2

First line reads a line from standard input and converts it to lowercase. Second line replaces all non-letter characters with spaces (last parameter is attribute characters; g means that all matches of regular expression are replaced, not only the first one). Third line capitalizes words in string, removes leading and trailing spaces and replaces all sequences of spaces with a single space. Finally, the spaces are removed from the string, and the result is printed to standard output.

import std.stdio;
import std.string;
import std.regexp;
 
void main() {
    string text = tolower(readln());
    text = sub(text,"[^a-z]"," ","g");
    text = capwords(text);
    text = sub(text," ","","g");
    writeln(text);
}