D Programming

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
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
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
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&mdash;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
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
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
Current D implementations compile directly into native code for efficient execution.

Getting and installing D
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)
Create a batch file "dmd_vars.bat" and move it to a directory that is included in the 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
/First Program Examples/

With Tango library the classic hello world program is:

/Alternate Program Examples/

With the Phobos library the classic hello world program is:

Compilation
Compiling hello world:

Reference manual to D

 * The official website
 * http://www.digitalmars.com/d/
 * Other websites
 * http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage
 * /Quick Reference Card/

All D features
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.


 * /Lexical/
 * /Modules/
 * /Declarations/
 * /Types/
 * /Properties/
 * /Attributes/
 * /Pragmas/
 * /Expressions/
 * /Statements/
 * /Arrays/
 * /Structs/
 * /Classes/
 * /Unions/
 * /Enums/
 * /Functions/
 * /Interfaces/
 * /Operator overloading/
 * /Templates/
 * /Mixins/
 * /Contracts/
 * /Conditional compilation/
 * /Exceptions/
 * /Garbage collection/
 * /Memory management/
 * /Floating point/
 * /Inline assembler/
 * /Documentation comments/
 * /Interfacing to C/
 * /Portability guide/
 * /Embedding D in HTML/
 * /Named character entities/
 * /Application Binary Interface/

D compilers

 * /GNU D Compiler/ (GDC)
 * /Digital Mars D compiler/ (DMD)
 * LLVM D Compiler (LDC)

D libraries

 * Libraries
 * Phobos - Standard D Runtime Library
 * Tango - D Runtime Library (beta)
 * Platform
 * Programming D in Windows
 * Programming D in Linux
 * Programming D in Macintosh
 * Programming D in xBSD

D GUI libraries

 * DFL - D Forms Library
 * gtkD - Wrapper for GTK+ (previously known as DUI)
 * wxD - Wrapper for wxWidgets
 * DWT - A D port of Java's SWT Toolkit
 * QtD - Wrapper for Qt Toolkit

Special

 * The D garbage collector
 * Using the Linux Realtime Application Interface (RTAI)

D言語 D Язык программирования D