D programming language

From Free net encyclopedia

(Difference between revisions)
Revision as of 02:20, 13 April 2006
Neilc (Talk | contribs)
rv: many (most!) languages have no "final" version. The fact that D is under development is made clear in the body of the article and doesn't warrant a prominent banner.
Next diff →

Current revision

D is an object-oriented, imperative systems programming language designed by Walter Bright of Digital Mars as a successor to [[C++]]. He has done this by adding some features and reducing the complexity of C++ syntax.

Contents

Features

D extends C++ by 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

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.

C++'s ABI is not supported, although D can access C++ code that is written to the C ABI, and can access C++ COM (Component Object Model) code.

Implementation

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

D is still under development, so changes to the language are made regularly. Some of these could break D programs written for older versions of the language and compiler. The official compiler by Walter Bright defines the language itself, and it is currently in the beta testing state.

Examples

Keywords are in blue, strings in red, comments in green.

Example 1

This example program prints its command line arguments. The main function is the entry point of a D program, and args is an array of strings representing the command line arguments. A string in D is an array of characters, represented by char[].

import std.stdio;       // for writefln()
int main(char[][] args)
{
   foreach(int i, char[] a; args)
      writefln("args[%d] = '%s'", i, a);
   return 0;
}

The foreach statement can iterate over any collection, in this case it is producing a sequence of keys (i) and values (a) from the array args.

Example 2

This illustrates the use of associative arrays to build much more complex data structures.

import std.stdio;       // for writefln()

int main()
{
  // Declare an associative array with string keys and
  // arrays of strings as data
  char[][] [char[]] container;

  // Add some people to the container and let them carry some items
  container["Anya"] ~= "scarf";
  container["Dimitri"] ~= "tickets";
  container["Anya"] ~= "puppy";

  // Iterate over all the persons in the container
  foreach (char[] person, char[][] items; container)
       display_item_count(person, items);
  return 0;
}

void display_item_count(char[] person, char[][] items)
{
  writefln(person, " is carrying ", items.length, " items.");
}

External links

Template:Wikibooks

de:D (Programmiersprache) es:Lenguaje de programación D fr:D (langage) ko:D 프로그래밍 언어 it:D (linguaggio di programmazione) nl:D (programmeertaal) ja:D言語 pl:D (język programowania) pt:Linguagem D ru:D (язык программирования) sk:D (programovací jazyk) fi:D (ohjelmointikieli) sv:D (programspråk)