Opentopia Directory Encyclopedia Tools

D programming language

Encyclopedia : D : DP : DPR : D programming language


D is an object-oriented, imperative system programming language designed by Walter Bright of Digital Mars as a re-engineering of C/C++. He has done this by re-designing many C++ features, and borrowing ideas from other programming languages.

Features

D was designed with lessons learned from C++. It uses many C++ concepts and adds to the functionality of C++ by also 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)

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()

void display_item_count(char[] person, char[][] items)

Example 3

This heavily annotated example highlights many of the enhancements over C++, while still retaining some of the better C++ aspects.

#!/usr/bin/dmd -run
/* sh style script syntax is supported! */
/* Hello World in D
* To compile:
*   dmd hello.d
* or to optimize:
*   dmd -O -inline -release hello.d
* or to get generated documentation:
*   dmd hello.d -D
*/

import std.stdio; // References to commonly used I/O routines.

void main(char[][] args) // 'void' for main means return 0 by default.

// Nested structs, classes and functions struct specs

specs argspecs(char[][] args) // Optional (built-in) function contracts. in out(result) body

// built-in string and common string operations, eg. '~' is concatenate. char[] argcmsg = "argc = %d"; char[] allocmsg = "allocated = %d"; writefln(argcmsg ~ ", " ~ allocmsg, argspecs(args).count,argspecs(args).allocated); }

/** * Stores a single command line argument. */ class CmdLin

public: /** * Object constructor. * params: * argc = ordinal count of this argument. * argv = text of the parameter */ this(int argc, char[] argv)

~this() // Object destructor

int argnum() // A property that returns arg number

char[] argv() // A property that returns arg text

wchar[] suffix() // A property that returns ordinal suffix return suffix; }

/** * A static property, as in C++ or Java, * applying to the class object rather than instances. * returns: The total number of commandline args added. */ static typeof(_totalc) total()

// Class invariant, things that must be true after any method is run. invariant }

See also

External links

Wikibooks has a manual, textbook or guide to this subject:

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: