D (programming language)

from Wikipedia, the free encyclopedia
D.
D Programming Language logo.svg
Basic data
Paradigms : imperative , object-oriented , functional , parallel , generic , modular
Publishing year: 2007
Designer: Walter Bright
Developer: Walter Bright,
Andrei Alexandrescu (from D 2.0)
Current  version : 2.091.1   (April 17, 2020)
Typing : Strong , static
Important implementations : DMD , GDC , LDC
Influenced by: C , C ++ , Eiffel , Java , C # , Python , Ruby
Affected: DScript, Genie, MiniD, Qore, Swift , Vala
Operating system : Platform independent
License : Boost software license
dlang.org

D is a programming language developed by Walter Bright since 1999 with object-oriented , imperative and declarative language elements. D was released on January 3, 2007 in the stable version 1.0. Outwardly, it is strongly based on C ++ , but without full linguistic compatibility with it.

Version 2 of D was developed from June 17, 2007 to the end of 2010, which adds new functionality such as closures and pure functions as well as the functional paradigm, but is not compatible with the predecessor. With the release of the new version, the book The D Programming Language by Andrei Alexandrescu , who played a key role in the design of the language, was also published.

Language resources

D adopts most of the language elements of the C language , but in contrast to C ++ does not have any compatibility with them. This is to avoid the adoption of design disadvantages. Thanks to ABI compatibility, all programs and libraries written in C can still be used. The connection of C ++ code, however, is subject to restrictions.

D is an object-oriented, imperative programming language, which from D 2.0 also offers the possibility of functional programming within an imperative programming language, and has class templates and operators that can be loaded . D offers design by contract and modules . Automatic garbage collection is unlike z. B. C / C ++ a fixed, albeit in principle an optional component of the language, whereby the plan is to decouple the runtime environment from it to the extent that working with D is also possible in situations where the use of a garbage collector is impossible or undesirable. However, automatic reference counting is not supported.

Programs can be written in D without a pointer . Fields consist transparently of both the location of their data and their length, which means that pointer arithmetic is superfluous and the permissibility of field accesses can be checked at runtime. In contrast to Java , however, it is still possible to use pointers when required, almost like in C, and to program them close to the machine.

Compiler

DMD, the Digital Mars D compiler, is the reference implementation by Walter Bright and is available for the x86 / x86-64 versions of Windows , Linux , macOS and FreeBSD .

The most important translators with alternative backends are GDC, which is based on GCC , and LDC, which is based on the qualities of LLVM . In addition to the high speed of the generated machine code, these backends also enable the operation of x86-64 and various other architectures.

In the meantime, two compilers for D in D have also been programmed: Dil and Dang , which are compatible with LLVM. A code generator for the .NET platform is more of a proof of concept than a functioning translator. The D language is supported since version 9.1 of the GCC.

Development environments

D is increasingly supported by various IDEs . The editors Entice Designer, Emacs , Vim , Scite , Scite4D, Smultron , TextMate , Zeus, Geany and Visual Studio Code are used . Vim and Scite4D support syntax highlighting and auto-completion . There is an extension for TextMate and Visual Studio Code, Code :: Blocks also partially supports D.

There are also plug-ins for other IDEs: Eclipse supports D with the DDT plug-in, Mono-D is available for MonoDevelop .

There are also IDEs written in D, such as Poseidon , which supports auto-completion and refactoring and has an integrated debugger . WinDbg and the GNU Debugger support D rudimentary.

Sample program

Hello World
Output of the command line parameters
// Programm, geschrieben in D2, das seine Parameter ausgibt
// importiert writefln aus dem Modul std.stdio
import std.stdio : writefln;

void main(string[] args) {
    // Jeder Eintrag im Feld args wird ausgegeben.
    foreach(i, arg; args)
        writefln("Parameter %d = '%s'", i, arg);
}

The command linemain parameters are passed to the function as an array of character strings. If you call this program under Windows , this text is displayed in a console window : beispiel.exe -win -s

Parameter 0 = 'beispiel.exe'
Parameter 1 = '-win'
Parameter 2 = '-s'
Unit tests

Unit tests or module tests are blocks that contain program code which is intended to test a function for different cases. In addition, the unit test code can be listed as an example in the documentation generated by DDOC. In D, the unit tests are carried out immediately before the main function.

/++
    Ermittelt, ob eine Zahl eine Primzahl ist.
    Hinweis: Das Verfahren ist nicht optimal.
Params:
    a: die zu testende Zahl
Returns:
    true, falls die Zahl eine Primzahl ist;
    false, ansonsten
+/
bool isPrime(long a) {
    if (a <= 1)
        return false;

    // falls a keine Primzahl ist und a restlos durch n teilbar ist
    for (long n = 2; n <= a / 2; ++n)
        if (a % n == 0)
            return false;

    // a war nicht teilbar -> a muss eine Primzahl sein
    return true;
}

unittest {
    // Die Bedingung in dem Assert-Aufruf muss erfüllt werden (wahr sein).
    // Sonst ist ein Fehler aufgetreten und das Programm bricht ab.
    assert(isPrime( 0) == false);
    assert(isPrime( 1) == false);
    assert(isPrime( 2) == true);
    assert(isPrime( 7) == true);
    assert(isPrime( 4) == false);
    assert(isPrime(10) == false);

    // Hier wird erwartet, dass der Test fehlschlägt, da 11 prim ist.
    // Fehlerhafter Code: Assert schlägt immer fehl!
    assert(isPrime(11) == false);
}

Unit tests are test functions that are supposed to test the behavior of a function for all possibilities. A compiler flag must be set so that unit tests can be executed (-unittest with DMD)

Generic programming

In addition to mixin -Templates the D programming language implements all common template types .

Function templates
// Diese Funktion liefert als Return-Wert das Maximum der beiden Parameter
// Der Template Datentyp T wird dabei automatisch (falls möglich) erkannt
T max(T)(T a, T b) {
    if( a < b )
        return b;
    else
        return a;
}

unittest {
    assert(max(2, 3) == 3);
    assert(max('a','c') == 'c');
    assert(max(3.1415, 2.61) == 3.1415);
    assert(max("Hallo", "Abc") == "Hallo");
}
Meta programming
// Berechnet das Ergebnis von basis^exp
template Pow(double basis, uint exp) {
    static if (exp > 0)
        enum Pow = basis * Pow!(basis, exp-1);
    else
        enum Pow = 1;
}

import std.stdio;

void main() {
    // Da pow ein Template ist, wird das Ergebnis zur Compilezeit bestimmt
    // und steht zur Laufzeit als Konstante zur Verfügung

    writeln("2.7182^3 = ", Pow!(2.7182, 3));
    writeln("3.1415^5 = ", Pow!(3.1415, 5));
}

Output:

2.7182^3 = 20.0837
3.1415^5 = 305.975
Compile time function execution

With functions (CTFE) still executed during compilation, initialization lists, lookup tables, etc. Ä. created and integrated by so-called mixins when translating D-code generated from strings . These two features offer a possibility for the realization of domain-specific languages in D.

T sum(T)(T[] elems) {
    T res;
    
    foreach(elem; elems)
        res += elem;
    
    return res;
}

enum arr = [10, 13, 14, 15, 18, 19, 22, 24, 25, 36];
// summeVonArr ist zur Kompilierungszeit bekannt.
enum summeVonArr = sum(arr); // == 196

// Statisches Integer-Array mit der Laenge 196
int[summeVonArr] staticArray;

literature

Web links

Individual evidence

  1. Change Log: 2.091.1 - D Programming Language
  2. ^ D Programming Language 1.0, Intro. Digital Mars
  3. FAQ - Is D open source?
  4. License in the compiler source code
  5. ^ License of the runtime library
  6. License of the standard library
  7. Heise Online: Programming more elegantly: D is here , January 3, 2007 - 14:52
  8. Interfacing to C ++
  9. Garbage Collection . dlang.org, accessed September 21, 2019.
  10. Michael Parker: Don't Fear the Reaper The D Blog, March 20, 2017, accessed September 21, 2019.
  11. Memory Management . in the D Wiki, accessed on September 21, 2019.
  12. DMD (Digital Mars D): DMD
  13. GDC (GNU D Compiler): GDC
  14. LDC on GitHub
  15. DIL project page
  16. Dang project page
  17. D Compiler for .NET
  18. ^ GCC 9 Release Series Changes, New Features, and Fixes
  19. DDT on Github
  20. Mono-D
  21. windbg Debugger on dlang.org, accessed on February 14, 2019.
  22. Debugging with GDB: D , accessed February 13, 2019.
  23. dlang.org-unit tests
  24. dlang.org-DDOC
  25. dlang.org mixin templates
  26. dlang.org templates
  27. dlang.org-CTFE
  28. dlang.org-mixins