The goal for this book is to create a complete, free, open-content, well-organized online book for the D programming language.

D is a programming language being designed as a successor to C++. Until this page gets better written and more informative, the D home can be found here.

Introduction

edit

This book aims at beginners learning D language. It will cover all the language basics and some design aspects. In addition it will introduce topics like multi-threading, GUI programming and standard library to get you started with real-world applications.

Overview

edit

To quote Walter Bright, the author of the D Programming Language:

D is a general purpose systems and applications programming language. It is a higher level language than C++, but retains the ability to write high performance code and interface directly with the operating system API's and with hardware. D is well suited to writing medium to large scale million line programs with teams of developers. D is easy to learn, provides many capabilities to aid the programmer, and is well suited to aggressive compiler optimization technology.

D is not a scripting language, nor an interpreted language. It doesn't come with a VM, a religion, or an overriding philosophy. It's a practical language for practical programmers who need to get the job done quickly, reliably, and leave behind maintainable, easy to understand code.

D is the culmination of decades of experience implementing compilers for many diverse languages, and attempting to construct large projects using those languages. D draws inspiration from those other languages (most especially C++) and tempers it with experience and real world practicality.


D is a statically-typed, multi-paradigm language supporting imperative programming, object-oriented programming, and template metaprogramming. It also supports generics and design by contract.

Main features of D

edit

D has many features not seen in C++, implementing design by contract, unit testing, true modules, automatic memory management (garbage collection), first class arrays, associative arrays, dynamic arrays, array slicing, nested functions, inner classes, closures (anonymous functions), and has a reengineered template syntax. D retains C++'s ability to do low-level coding, and adds to it with support for an integrated inline assembler. C++ multiple inheritance is replaced by single inheritance with interfaces and mixins. D's declaration, statement and expression syntax closely matches that of C++.

The inline assembler typifies the differentiation between D and application languages like Java and C#. An inline assembler allows a programmer to enter machine-specific assembly code alongside standard D code—a technique often used by systems programmers to access the low-level features of the processor needed to run programs that interface directly with the underlying hardware, such as operating systems and device drivers.

Built into the language is a documentation generator called Ddoc.

Memory management

edit

Memory is usually managed with garbage collection, but specific objects can be finalized immediately when they go out of scope. Explicit memory management is possible using the overloaded operators new and delete, as well as simply calling C's malloc and free directly. It is also possible to disable garbage collection for individual objects, or even for the entire program if more control over memory management is desired.

Interaction with other systems

edit

C's ABI (Application Binary Interface) is supported as well as all of C's fundamental and derived types, enabling direct access to existing C code and libraries. C's standard library is part of standard D.

In D 1.0, C++'s ABI is not supported, although it can access C++ code that is written to the C ABI, and can access C++ COM (Component Object Model) code. D 2 already supports some interaction with the C++ ABI.

Implementation

edit

Current D implementations compile directly into native code for efficient execution.

Getting and installing D

edit

The Digital Mars D compiler can be obtained from the digital mars website. http://www.digitalmars.com/d/download.html You will need the two files dmd.zip and dmc.zip. According to the manual both files should be extracted into a root directory or one without spaces or other special characters. The location of link.exe should then be added to the path. D programs can now be compiled by calling 'dmd'.

Win32: Example of configuration 1 (dmd)

edit

Create a batch file "dmd_vars.bat" and move it to a directory that is included in the path:

@echo off

echo Setting up a dmd environment...

set PATH=c:\dm\bin;c:\dmd\bin
rem ;%SystemRoot%\System32
set LIB=c:\dmd\lib;c:\dm\lib

echo PATH set to %PATH%

Then:

  • Open a Command Window (cmd.exe)
  • Call "dmd_vars.bat" before compiling something
  • Call "dmd hello.d -ofhello_world" for example, which creates "hello_world.exe"

Note: This works well, even if you have other compilers/tools installed (which might also have link.exe/make.exe etc.)

Your first D programs

edit

First Program Examples

With Tango library the classic hello world program is:

 import tango.io.Console;
 void main() 
 { 
  Cout("Hello, World").newline;
 }

Alternate Program Examples

With the Phobos library the classic hello world program is:

 import std.stdio;
 void main() 
 { 
  writefln("Hello, World");
 }

Compilation

edit

Compiling hello world:

dmd hello.d -ofhello
gdc hello.d -o hello

Reference manual to D

edit

All D features

edit

This is an incomplete list of all D features. It is specifically created to show and teach the D programming language with lots of examples.

D compilers

edit

D libraries

edit

D GUI libraries

edit

Special

edit