D (programming language)
From Wikipedia, the free encyclopedia
| D programming language | |
|---|---|
| Paradigm | multiparadigm: object-oriented, imperative |
| Appeared in | 1999 |
| Designed by | Walter Bright |
| Latest release | 1.030 (stable)/ May 16, 2008[1] |
| Typing discipline | strong, static |
| Major implementations | DMD, GDC |
| Influenced by | C, C++, C#, Java, Eiffel |
The D programming language, also known simply as D, is an object-oriented, imperative, multiparadigm system programming language by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is predominantly influenced by that language, it is not a variant of C++. D has redesigned some C++ features and has been influenced by concepts used in other programming languages, such as Java, C# and Eiffel. A stable version, 1.0, was released on January 2, 2007. An experimental version, 2.0, was released on June 17, 2007.
Contents |
[edit] Features
D is being designed with lessons learned from practical C++ usage rather than from a theoretical perspective. Even though it uses many C/C++ concepts it also discards some, and as such is not strictly backward compatible with C/C++ source code. It 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 [1], anonymous functions, compile time function execution, lazy evaluation 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 Java style single inheritance with interfaces and mixins. D's declaration, statement and expression syntax closely matches that of C++.
The inline assembler typifies the differences between D and application languages like Java and C#. An inline assembler lets programmers enter machine-specific assembly code in with standard D code—a technique often used by system 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.
D has built-in support for documentation comments, but so far only the compiler supplied by Digital Mars implements a documentation generator.
[edit] Programming paradigms
D supports three main programming paradigms—imperative, object-oriented, and metaprogramming.
[edit] Imperative
Imperative programming in D is almost identical to C. Functions, data, statements, declarations and expressions work just as in C, and the C runtime library can be accessed directly. Some notable differences between D and C in the area of imperative programming include D's foreach loop construct, which allows looping over a collection, and nested functions, which are functions that are declared inside of another and may access the enclosing function's local variables.
[edit] Object Oriented
Object oriented programming in D is based on a single inheritance hierarchy, with all classes derived from class Object. Multiple inheritance is possible from interfaces (interfaces are a lot like C++ pure abstract classes).
[edit] Metaprogramming
Metaprogramming is supported by a combination of templates, compile time function execution, tuples, and string mixins. The following examples demonstrate some of D's compile-time features.
Templates in D can be written in a more function-like style than those in C++. Here the use of static if, D's compile-time conditional construct, is demonstrated to construct a factorial template.
template Factorial(ulong n) { static if( n <= 1 ) const Factorial = 1; else const Factorial = n * Factorial!(n-1); }
This is a regular function that performs the same calculation. The template version's code is similar to that of this function.
ulong factorial(ulong n) { if( n <= 1 ) return 1; else return n * factorial(n-1); }
In the following two examples, the template and function defined above are used to compute factorials. The types of constants need not be specified explicitly as the compiler infers their types from the right-hand sides of assignments.
const fact_7 = Factorial!(7);
This is an example of compile-time function execution. Ordinary functions may be used in constant, compile-time expressions provided they meet certain criteria.
const fact_9 = factorial(9);
The std.metastrings.Format template performs printf-like data formatting, and the "msg" pragma displays the result at compile time.
import std.metastrings; pragma(msg, Format!("7! = %s", fact_7)); pragma(msg, Format!("9! = %s", fact_9));
[edit] 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, and by simply calling C's malloc and free directly. Garbage collection can be controlled: programmer can add and exclude memory ranges from being observed by collector, can pause and resume collector and force generational or full collection cycle[2]. The manual gives many examples of how to implement different highly optimized memory management schemes for when garbage collection is inadequate in a program.
[edit] Interaction with other systems
C's application binary interface (ABI) 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. Unless you use very explicit namespaces it can be somewhat messy to access, as it is spread throughout the D modules that use it -- but the pure D standard library is usually sufficient unless interfacing with C code.
C++'s ABI is not fully supported, although D can access C++ code that is written to the C ABI, and can access C++ COM (Component Object Model) code. The D parser understands an extern (C++) calling convention for linking to C++ objects, but it is only implemented in the currently experimental D 2.0.
[edit] D 2.0
D 2.0, a branch version of D that includes experimental features, was released on June 17, 2007. Some of these features are:
- Support for enforcing const-correctness:
- D differentiates between mutable references to immutable data, const references to mutable data, and combinations thereof
constandinvariantkeywords are transitive
- Limited support for linking with code written in C++
- Iteration with
foreachover defined range only - Support for "real" closures. Previously closures couldn't be safely returned from functions, because stack-allocated variables would become inaccessible
- Changes to standard Phobos library
| Please help improve this section by expanding it. Further information might be found on the talk page or at requests for expansion. |
[edit] Implementation
Current D implementations compile directly into machine code for efficient execution.
Even though D is still under development, changes to the language are no longer made regularly since version 1.0 of January 2, 2007. The design is currently virtually frozen, and newer releases focus on resolving existing bugs. Version 1.0 is not completely compatible with older versions of the language and compiler. The official compiler by Walter Bright defines the language itself.
- DMD: the Digital Mars D compiler, the official D compiler by Walter Bright. The compiler front end is licensed under both the Artistic License and the GNU GPL; sources for the front end are distributed along with the compiler binaries. The compiler back end is proprietary.
- GDC: A front end for the GCC back end, built using the open DMD compiler sources. Development snapshots also support D version 2.0.
- LLVMDC: A new front end, also based on open DMD sources that uses LLVM as its compiler back end. It's in early development.
[edit] Development tools
D is still lacking support in many IDEs, which is a potential stumbling block for some users. Editors used include Entice Designer, emacs, vim, SciTE and Smultron among others. Vim supports both syntax highlighting and code completion (through patched ctags). A bundle is available for TextMate, and the Code::Blocks IDE includes partial support for the language. However, standard IDE features such as code completion or refactoring are not yet available, though they do work partially in Code::Blocks (due to D's similarity to C).
There are at least two actively developed Eclipse plug-ins for D, Descent and Mmrnmhrm.
Additionally, there are open source D IDEs written in the D language itself such as Poseidon, which does feature code completion, syntax highlighting, and integrated debugging.
D applications can be debugged using any C/C++ debugger, like GDB or WinDbg, although support for various fundamental language features is extremely limited. A debugger with explicit support for D is Ddbg for Windows. The commercial ZeroBUGS debugger for Linux has experimental support for the D language. Ddbg can be used with various IDEs or from the command line; ZeroBUGS has its own GUI.
[edit] Problems and controversies
[edit] Operator overloading
D operator overloads are significantly less powerful than the C++ counterparts. A popular example is the opIndex, which does not allow returning references. This makes assignments like obj[i] = 5; impossible. The D solution is the opIndexAssign operator, which only fixes this very case, but not variations like obj[i] += 5;. In addition, the C++ way of returning a reference allows for the usage of the returned type's overloaded assignment operator. This is currently not possible in D. D 2.0 will fix this by introducing an opIndexLvalue - like operator overload, and deprecating opIndexAssign.[citation needed]
[edit] Division concerning the standard library
The standard library in D is called Phobos. Some members of the D community think Phobos is too simplistic and that it has numerous quirks and other issues, and a replacement of the library called Tango was written.[2] However, Tango and Phobos are at the moment incompatible due to different run-time libraries (the garbage collector, threading support, etc). The existence of two libraries, both widely in use, could lead to significant problems where some packages use Phobos and others use Tango.
[edit] Unfinished support for shared/dynamic libraries
Unix's ELF shared libraries are supported to an extent using the GDC compiler. On Windows systems, DLLs are supported and allow D's garbage collector allocated objects to be safely passed to C functions, since the garbage collector scans the stack for pointers. However, there are still limitations with DLLs in D including the fact that run-time type information of classes defined in the DLL is incompatible with those defined in the executable, and that any object created from within the DLL must be finalized before the DLL is unloaded.[3]
[edit] Other
D has no built-in support for weak references, although there are some libraries that implement them. Support for Unicode strings is incomplete (compiler accepts Unicode source code, standard library and foreach constructs operate on UTF-8, but string slicing and length property operate on bytes rather than characters).
[edit] Examples
[edit] 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[] in D 1.0, or invariant(char)[] in D 2.0 alpha. Newer versions of the language define string as an alias for char[] or invariant(char) [], however, an explicit alias definition is necessary for compatibility with older versions.
import std.stdio: writefln; void main(string[] args) { foreach(i, a; args) writefln("args[%d] = '%s'", i, a); }
The foreach statement can iterate over any collection, in this case it is producing a sequence of indexes (i) and values (a) from the array args. The index i and the value a have their types inferred from the type of the array args.
[edit] Example 2
This illustrates the use of associative arrays to build much more complex data structures.
import std.stdio: writefln; void main(string[] args) { // Declare an associative array with string keys and // arrays of strings as data string[][string] people; // Add some people to the container and let them carry some items people["Anya"] ~= "scarf"; people["Dimitri"] ~= "tickets"; people["Anya"] ~= "puppy"; // Iterate over all the persons in the container // and display the number of items each person is carrying foreach (person, items; people) writefln(person, " is carrying ", items.length, " items."); }
[edit] See also
[edit] References
[edit] External links
- Digital Mars: D programming language
- D at the Open Directory Project
- DSource, an open source community for the D Programming Language.
- Dprogramming.com, home of the DFL windowing library.
- Wiki4D, "the wiki for the d programming language"
- gdc, D front-end for GCC
- The Computer Language Benchmarks Game
- D Documentation Wiki
- D Language Feature Table
- Video presentation of D by Walter Bright
- Ddbg - Win32 D debugger
- DWin - library for D Programming Language
- DLogo - logos, buttons, banners for D Programming Language
- SciTE4D - Text Editor for D Programming Language
- D Programming Language China
- Chinese support forum for D Programming Language
|
|||||||||||||||||

