D Programming Language Articles about
D Programming Language
Website Links For
Programming
 

Information About

D Programming Language





Programming Information

  Name D
  Logo
  Paradigm Multiparadigm
  Year 1999
  Typing strong, static
  Designer Walter Bright
  Latest Release Version 1021 (stable)
  Latest Release Date Aug 26 2007 D Change Log, http://wwwdigitalmarscom/d/10/changeloghtml
  Implementations DMD , GDC
  Influenced By C , C++ , C# , Java , Eiffel


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 .


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 Test ing, true Modules , Automatic Memory Management (garbage collection), First Class Array s, Associative Array s, Dynamic Array s, Array Slicing , Nested Function s, Inner Class es, limited form of Closure s, 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 Mixin s. 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 System s and Device Driver s.

D has built-in support for documentation comments, but so far only the compiler supplied by Digital Mars implements a Documentation Generator .


Programming paradigms


D supports three main programming paradigms—imperative, object-oriented, and metaprogramming.


Imperative


Imperative programming is almost identical to C. Functions, data, statements, declarations and expressions work just as C, and the C runtime library can be accessed directly.


Object oriented


OO 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++ abstract classes).


Metaprogramming


Metaprogramming is supported by a combination of templates, compile time function execution, tuples, and string mixins.


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 disabled for individual objects, or even for a full program, if more control over memory management is desired. The manual gives many examples of how to implement different highly optimized memory management schemes for when garbage collection is inadequate in a program.


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 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 not yet implemented.


IMPLEMENTATION

Current D implementations Compile directly into Native 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 Compiler : 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 : D Compiler, built using the DMD compiler front end and the GCC back end.



DEVELOPMENT TOOLS


D is still lacking support in many IDEs, which is a potential stumbling block for some users. Editors used include Entice Designer , IDE includes partial support for the language. However, standard IDE features such as Code Completion or Refactoring are not yet available.

There have been several Eclipse plug-in for D (e.g. DDT) under development. But according to prowiki's wiki4d , descent seems the only one actively being developed as of July 2007.

Additionally, there are , Syntax Highlighting , and integrated Debugging .

D applications can be debugged with s for C also work with D since it uses the same Object File format. However, debugging features are extremely limited with those.


EXAMPLES


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 Newer versions of the language define string as an alias for char[ , however, an explicit alias definition is necessary for compatibility with older versions.


import std.stdio; // for writefln()
alias char {Link without Title} string; // for compatibility with older compilers; newer compilers define this implicitly
int main(string {Link without Title} args)
{
foreach(i, a; args)
writefln("args {Link without Title} = '%s'", i, a);
return 0;
}


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.


Example 2

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


import std.stdio; // for writefln()
alias char {Link without Title} string; // for compatibility with older compilers; newer compilers define this implicitly

int main(string {Link without Title} args)
{
// Declare an associative array with string keys and
// arrays of strings as data
string [string container;

// Add some people to the container and let them carry some items
container {Link without Title} ~= "scarf";
container {Link without Title} ~= "tickets";
container {Link without Title} ~= "puppy";

// Iterate over all the persons in the container
foreach (string person, string {Link without Title} items; container)
display_item_count(person, items);
return 0;//success
}

void display_item_count(string person, string {Link without Title} items)
{
writefln(person, " is carrying ", items.length, " items.");
}



Example 3

This heavily annotated example highlights many of the differences from C++, while still retaining some 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.
alias char {Link without Title} string; // for compatibility with older compilers; newer compilers define this implicitly

int main(string {Link without Title} args)
{
// 'writefln' (Write-Formatted-Line) is the type-safe 'printf'
writefln("Hello World, " // automatic concatenation of string literals
"Reloaded");

// Strings are denoted as a dynamic array of chars 'char {Link without Title} ', aliased as 'string'
// auto type inference and built-in foreach
foreach(argc, argv; args)
{
auto cl = new CmdLin(argc, argv); // OOP is supported
writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv); // user-defined class properties.

delete cl; // Garbage Collection or explicit memory management - your choice
}

// Nested structs, classes and functions
struct specs
{
// all vars automatically initialized to 0 at runtime
int count, allocated;
// however you can choose to avoid array initialization
int {Link without Title} bigarray = void;
}

specs argspecs(string {Link without Title} args)
// Optional (built-in) function contracts.
in
{
assert(args.length > 0); // assert built in
}
out(result)
{
assert(result.count == CmdLin.total);
assert(result.allocated > 0);
}
body
{
  • s = new specs;

  • // no need for '->'

s.count = args.length; // The 'length' property is number of elements.
s.allocated = typeof(args).sizeof; // built-in properties for native types
foreach(arg; args)


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

  • ---

  • Stores a single command line argument.

  • /

  • class CmdLin

{
private
{
int _argc;
string _argv;
static uint _totalc;
}

public:
  • ---

  • Object constructor.

  • params:

  • argc = ordinal count of this argument.

  • argv = text of the parameter

  • /

  • this(int argc, string argv)

{
_argc = argc + 1;
_argv = argv;
_totalc++;
}

~this() // Object destructor
{
// Doesn't actually do anything for this example.
}

int argnum() // A property that returns arg number
{
return _argc;
}

string argv() // A property that returns arg text
{
return _argv;
}

wstring suffix() // A property that returns ordinal suffix
{
wstring suffix; // Built in Unicode strings (UTF-8, UTF-16, UTF-32)
switch(_argc)
{
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default: // 'default' is mandatory with "-w" compile switch.
suffix = "th";
}
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()

{
return _totalc;
}

// Class invariant, things that must be true after any method is run.
invariant ()
{
assert(_argc > 0);
assert(_totalc >= _argc);
}
}



Example 4

This example demonstrates some of the power of D's compile-time features.



  • Templates in D are much more powerful than those in C++. Here we can see

  • the use of static if, D's compile-time conditional construct, to easily

  • construct a factorial template.

  • /

  • template Factorial(ulong n)

{
static if( n <= 1 )
const Factorial = 1;
else
  • Factorial!(n-1);

  • }



  • Here is a regular function that performs the same calculation. Notice how

  • similar they are.

  • /

  • ulong factorial(ulong n)

{
if( n <= 1 )
return 1;
else
  • factorial(n-1);

  • }



  • Finally, we can compute our factorials. Notice that we don't need to

  • specify the type of our constants explicitly: the compiler is smart enough

  • to fill in the blank for us, since it already knows the type of the

  • right-hand side of the assignment.

  • /

  • const fact_7 = Factorial!(7);



  • This is an example of compile-time function evaluation: ordinary functions

  • may be used in constant, compile-time expressions provided they meet

  • certain criteria.

  • /

  • const fact_9 = factorial(9);



  • Here we can see just how powerful D's templates are: we are using the

  • std.metastrings.Format template to perform printf-style data formatting,

  • and displaying the result using the message pragma.

  • /

  • import std.metastrings;

pragma(msg, Format!("7! = %s", fact_7));
pragma(msg, Format!("9! = %s", fact_9));


  • Our task done, we can forcibly stop compilation. This program need never

  • actually be compiled into an executable!

  • /

  • static assert(false, "My work here is done.");




SEE ALSO



REFERENCES



EXTERNAL LINKS