D Language

Topics related to D Language:

Getting started with D Language

D is a systems programming language with C-like syntax and static typing. It combines efficiency, control and modeling power with safety and programmer productivity.

Dynamic Arrays & Slices

Slices generate a new view on existing memory. They don't create a new copy. If no slice holds a reference to that memory anymore - or a sliced part of it - it will be freed by the garbage collector.

Using slices it's possible to write very efficient code for e.g. parsers that just operate on one memory block and just slice the parts they really need to work on - no need allocating new memory blocks.

Ranges

If a foreach is encountered by the compiler

foreach (element; range) {

it's internally rewritten similar to the following:

for (auto it = range; !it.empty; it.popFront()) {
    auto element = it.front;
    ...
}

Any object which fulfills the above interface is called an input range and is thus a type that can be iterated over:

struct InputRange {
    @property bool empty();
    @property T front();
    void popFront();
}

Associative Arrays

Traits

Templates

Structs

UFCS - Uniform Function Call Syntax

In a call a.b(args...), if the type a does not have a method named b, then the compiler will try to rewrite the call as b(a, args...).

Scope guards

Imports and modules

Modules automatically provide a namespace scope for their contents. Modules superficially resemble classes, but differ in that:

  • There's only one instance of each module, and it is statically allocated.
  • There is no virtual table.
  • Modules do not inherit, they have no super modules, etc.
  • Only one module per file.
  • Module symbols can be imported.
  • Modules are always compiled at global scope, and are unaffected by surrounding attributes or other modifiers.
  • Modules can be grouped together in hierarchies called packages.

Modules offer several guarantees:

  • The order in which modules are imported does not affect the semantics.
  • The semantics of a module are not affected by what imports it.
  • If a module C imports modules A and B, any modifications to B will not silently change code in C that is dependent on A.

Compile Time Function Evaluation (CTFE)

Classes

Loops

Strings

  • Strings in D are immutable; use .dup to make a mutable char array if you want to edit in-place.

Unittesting

Memory & Pointers

Contracts

The assertions will be optimized away in an release build.