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; short s = 5; int i; long l; cent c; ubyte b = -1 ushort; uint; ulong; ucent;
|
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; real r; ifloat if; idouble id; ireal ir; cfloat cf; cdouble cd; creal cr;
|
Character data types
char
ch; // unsigned 8 bit UTF-8
wchar wch; dchar dch;
|
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 ( ) statement; 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:
or:
while ( ) { statement1; statement2; }
|
Example:
int i = 0; while (i < 4) { writefln("i = %d", i); i++; }
|
Output:
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:
-
type
is the data type specifier of the data returned by the
function
-
name
is the identifier by which it will be possible to call
the
function
-
parameters
(as many as needed): Each parameter consists of a
data
type specifier followed by an identifier, like any regular variable
declaration (for example: int x) and which acts within the function as
a regular local variable. They allow to pass arguments to the function
when it is called. The different parameters are separated by commas.
-
statements
is the function's body. It is a block of
statements
surrounded by braces {
}.
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];
-
type
is the data type specifier of the elements in the array
-
name
is the identifier by which it will be possible to access the array
elements
-
size
is an integer constant. It is the number of elements of the array
Example:
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