article

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 , strings in , comments in .

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*.

std.stdio; main(** args) { ( i, * a; args) writefln(, i, a); 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.

std.stdio; main() { *" target="_blank" >* container; container* ~= ; container* ~= ; container* ~= ; (person, * items; container) display_item_count(person, items); 0; } display_item_count(person, * items) { writefln(person, , items.length, ); }

Example 3

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

std.stdio; main(** args) { writefln( ); (argc, argv; args) { cl = CmdLin(argc, argv); writefln(cl.argnum, cl.suffix, , cl.argv); delete cl; } specs { count, allocated; } specs argspecs(** args) { (args.length > 0); } (result) { (result.count == CmdLin.total); (result.allocated > 0); } { specs* s = specs; s.count = args.length; s.allocated = (args).sizeof; (arg; args) s.allocated += arg.length * (arg*).sizeof; *s; } * argcmsg = ; * allocmsg = ; writefln(argcmsg ~ ~ allocmsg, argspecs(args).count,argspecs(args).allocated); } CmdLin { { _argc; * _argv; _totalc; } : ( argc, * argv) { _argc = argc + 1; _argv = argv; _totalc++; } () { } argnum() { _argc; } * argv() { _argv; } * suffix() { * suffix; (_argc) { 1: suffix = ; ; 2: suffix = ; ; 3: suffix = ; ; : suffix = ; } suffix; } (_totalc) total() { _totalc; } { (_argc > 0); (_totalc >= _argc); } }

See also


External links


C programming language family | Class-based programming languages | Curly bracket programming languages | Multi-paradigm programming languages | Systems programming languages

D (език за програмиране) | D (Programmiersprache) | Lenguaje de programación D | D (langage) | D 프로그래밍 언어 | D (linguaggio di programmazione) | D (programmeertaal) | D言語 | D (język programowania) | Linguagem D | D (язык программирования) | D (programovací jazyk) | D (ohjelmointikieli) | D (programspråk)

 

This article is licensed under the GNU Free Documentation License. It uses material from the "D programming language".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld