NETB131 Programming Project
Programming language D
An overview

Zdravko Sashkov Iskrenov, F40996

1. History and Special Features

    The D programming language, also known simply as D, is an object-oriented, imperative, multiparadigm system programming language by Walter Bright of Digital Mars and . 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.

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, 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.
D supports three main programming paradigms—imperative, object-oriented, and metaprogramming.

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 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 is supported by a combination of templates, compile time function execution, tuples, and string mixins.


2. "Hello World" Program

import std.stdio;

void main()
{
    writefln("Hello World!");
}


3. Fundamental Data Types (integer, floating point, string) and Assignment Operator
Definitions:

Integer data types

Integer data types All integer types represent a signed or unsigned number with 2^x bit (with 2 < x < 8).
The default initialised value is always zero.

Example:

byte b; // signed 8 bits [-128 .. +127] is zero.
short s = 5; // signed 16 bits [-32768.. +32767] is five.
int i; // signed 32 [-4294967296 .. +4294967295]
long l; // signed 64 [-1.8e19 .. +1.8e19]
cent c; // signed 128 [-1,7e+38 .. +1,7e+38] is also zero.

ubyte b = -1 // unsigned 8 bits [0 .. +255] is 255!
ushort;
uint;
ulong; // unsigned 64 [0 .. 18446744073709551616]
ucent; // unsigned 128 [0 .. 3,4e+38]


Floatingpoint data types


All floatingpoint data types can be used for signed numbers.
The default initialisied value is NaN, NaN * 1.0i (for imaginary) or NaN+NaN * 1.0i (for complex)

Note: Real implementation on Intel CPUs is 80 bits.

float f; // 32 bit floating point
double d; // 64 bit floating point
real r; // largest hardware implemented floating point

ifloat if; //imaginary float
idouble id; // imaginary double
ireal ir; // imaginary real

cfloat cf; // complex number of two float values
cdouble cd; // complex double
creal cr; // complex real

Character data types

char ch; // unsigned 8 bit UTF-8
wchar wch; // unsigned 16 bit UTF-16
dchar dch; // unsigned 32 bit UTF-32
If you want to use strings, create an array of char.


Assignment Operator
The = operator assigns the value of the expression on the right to the variable on the left.

total = pennies * 0.8;
total = count * 0.2 + pennies


4. Basic Control Flow (conditional and loop statements)

    Conditional operator:

if ( /* boolean expression*/ )
statement;
// Optional:
else
statement;


"If" executes the next statement only if the boolean expression evaluates to "true". If there is an "else" block, it gets executed if the expression evaluates to "false".
The statements that are executed are often block statements.

Example:

if (i == 4)
{
writef("i is equal to 4");
} else {
writef("i is not equal to 4");
}


    Loop operator:

while ( /* boolean expression */ )
{
statement;
}


or:


while ( /* boolean expression */ )
{
statement1;
statement2;
// ...
}


Example:

int i = 0;
while (i < 4)
{
writefln("i = %d", i);
i++;
}

Output:

i = 0
i = 1
i = 2
i = 3




    Task: Input an integer number n and output the sum: 1+22+32+...+n2. Use input validation for n to be positive.

import std.stdio;
import std.stream;

int main()
{
int n;
scanf("%lf", &n);
if (n < 0) return 1;
int sum = 0;
int i = 0;
while (i <= n) sum += i*i;
writefln(sum);
return 0;
}


5. Functions - syntax, writing and using functions, example
The following is function's format:
type name ( parameter1, parameter2, ...) { statements }
where: Example:

import std.stdio;

uint squareIt(uint x) {
return x * x;
}

void main() {
uint i;
writefln(i, "\t(initial value)");

i = squareIt(3);
writefln(i, "\t(3 * 3)");

i = squareIt(4);
writefln(i, "\t(4 * 4)");

i = squareIt(5);
writefln(i, "\t(5 * 5)");
}



6. Arrays - syntax, definition, example
type name[size];

import std.stdio;

void main()
{
/*
Believe it or not, but these two arrays
have the same dimensions.
*/

int[20][2] whatever;
int whatever2[2][20];

whatever[0][19] = 2;
whatever2[0][19] = 2;
}


7. Compilers
8. Projects and Software in D

  • Application software
  • A list of some projects written in D language

  • 9. Standard
         D has redesigned some C++ features and has been influenced by concepts used in other programming languages, such as Java, C# and Eiffel so           any standard applied to those languages can be used when coding in D language.


    10. References
    [1] D Documentation
    [2] DSource, an open source community for the D Programming Language
    [3] Wikipedia - D (programming language)
    [4] Digital Mars