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.
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.
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();
}
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...)
.
Using scope guards makes code much cleaner and allows to place resource allocation and clean up code next to each other. These little helpers also improve safety because they make sure certain cleanup code is always called independent of which paths are actually taken at runtime.
The D scope feature effectively replaces the RAII idiom used in C++ which often leads to special scope guards objects for special resources.
Scope guards are called in the reverse order they are defined.
Modules automatically provide a namespace scope for their contents. Modules superficially resemble classes, but differ in that:
Modules offer several guarantees:
CTFE is a mechanism which allows the compiler to execute functions at compile time. There is no special set of the D language necessary to use this feature - whenever a function just depends on compile time known values the D compiler might decide to interpret it during compilation.
You can also play interactively with CTFE.
See the specification, browse a book chapter on classes, inheritance and play interactively.
for
loop in Programming in D, specificationwhile
loop in Programming in D, specificationdo while
loop in Programming in D, specificationforeach
in Programming in D, opApply, specification.dup
to make a mutable char
array if you want to edit in-place.The assertions will be optimized away in an release build.