D Programming
This page was imported and needs to be de-wikified. Books should use wikilinks rather sparsely, and only to reference technical or esoteric terms that are critical to understanding the content. Most if not all wikilinks should simply be removed. Please remove {{dewikify}} after the page is dewikified. |
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 | edit source]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 | edit source]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.
This section is a stub. You can help Wikibooks by expanding it. |
Main features of D
[edit | edit source]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 | edit source]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 | edit source]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 | edit source]Current D implementations compile directly into native code for efficient execution.
Getting and installing D
[edit | edit source]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 | edit source]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 | edit source]With Tango library the classic hello world program is:
import tango.io.Console;
void main()
{
Cout("Hello, World").newline;
}
With the Phobos library the classic hello world program is:
import std.stdio;
void main()
{
writefln("Hello, World");
}
Compilation
[edit | edit source]Compiling hello world:
dmd
[edit | edit source]dmd hello.d -ofhello
gdc
[edit | edit source]gdc hello.d -o hello
Reference manual to D
[edit | edit source]- The official website
- Other websites
- Quick Reference Card
All D features
[edit | edit source]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
[edit | edit source]- GNU D Compiler (GDC)
- Digital Mars D compiler (DMD)
- LLVM D Compiler (LDC)
D libraries
[edit | edit source]- Libraries
- Platform
- Programming D in Windows
- Programming D in Linux
- Programming D in Macintosh
- Programming D in xBSD
D GUI libraries
[edit | edit source]- 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