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.

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.

Version 1 of D is stable, Version 2 is (as of November 2009) approaching completion.

A K&R type book for D, “The D Programming Language”, written by Andrei Alexandrescu and published by Addison-Wesley Professional is scheduled for publication in May 2010. It will cover D2.

A shorter book — “Learn to Tango with D” by Kris Bell), Lars Ivar Igesund, Sean Kelly, and Michael Parker — was published by APress 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 var = value
Variable declaration type var
Variable declaration with assignment type var = 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) statement
If - then - else if (condition) statement else statement
Loop forever while (true) statement
While condition do while (condtion) statement
Do until condition do statement while (condtion)
For each value in a numeric range, 1 increment for (int i = first; i < limit ; i++) statement
For each value in a numeric range, 1 decrement for (int i = limit - 1; i >= first; i--) statement

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.

module factorial;

import std.stdio;

ulong iterative(ulong x)
{
	ulong result = 1;
	
	for (ulong count = 1; count <= x; ++count)
	{
		result *= count;
	}
	return result;
}

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