Paradigm | Multi-paradigm: functional, imperative, object-oriented |
---|---|
Designed by | Walter Bright, Andrei Alexandrescu (since 2007) |
Developer | D Language Foundation |
First appeared | 8 December 2001; 17 years ago[1] |
Stable release | |
Typing discipline | Inferred, static, strong |
OS | FreeBSD, Linux, macOS, Windows |
License | Boost[3][4][5] |
Filename extensions | .d |
Website | dlang.org |
Major implementations | |
DMD (reference implementation), GDC, LDC, SDC | |
Influenced by | |
C, C++, C#, Eiffel,[6]Java, Python | |
Influenced | |
Genie, MiniD, Qore, Swift,[7]Vala | |
|
foreach
loop construct, which allows looping over a collection, and nested functions, which are functions that are declared inside of another and may access the enclosing function's local variables.static if
, D's compile-time conditional construct, is demonstrated to construct a template that performs the same calculation using code that is similar to that of the function above:std.string.format
function performs printf
-like data formatting (also at compile-time, through CTFE), and the 'msg' pragma displays the result at compile time:function
, which is simply a pointer to a stack-allocated function, and delegate
, which also includes a pointer to the surrounding environment. Type inference may be used with an anonymous function, in which case the compiler creates a delegate
unless it can prove that an environment pointer is not necessary. Likewise, to implement a closure, the compiler places enclosed local variables on the heap only if necessary (for example, if a closure is returned by another function, and exits that function's scope). When using type inference, the compiler will also add attributes such as pure
and nothrow
to a function's type, if it can prove that they apply.std.functional
and std.algorithm
.new
and delete
, and by simply calling C's malloc and free directly. Garbage collection can be controlled: programmers may add and exclude memory ranges from being observed by the collector, can disable and enable the collector and force either a generational or full collection cycle.[12] The manual gives many examples of how to implement different highly optimized memory management schemes for when garbage collection is inadequate in a program.[13]@safe
are checked at compile time to ensure that they do not use any features that could result in corruption of memory, such as pointer arithmetic and unchecked casts, and any other functions called must also be marked as @safe
or @trusted
. Functions can be marked @trusted
for the cases where the compiler cannot distinguish between safe use of a feature that is disabled in SafeD and a potential case of memory corruption.[15]main
function is the entry point of a D program, and args
is an array of strings representing the command line arguments. A string
in D is an array of characters, represented by char[]
in D1, or immutable(char)[]
in D2.foreach
statement can iterate over any collection. In this case, it is producing a sequence of indexes (i
) and values (arg
) from the array args
. The index i
and the value arg
have their types inferred from the type of the array args
.words.txt
, which contains a different word on each line, and prints all the words that are anagrams of other words.signs2words
is a built-in associative array that maps dstring (32-bit / char) keys to arrays of dstrings. It is similar to defaultdict(list)
in Python.lines(File())
yields lines lazily, with the newline. It has to then be copied with idup
to obtain a string to be used for the associative array values (the idup
property of arrays returns an immutable duplicate of the array, which is required since the dstring
type is actually immutable(dchar)[]
). Built-in associative arrays require immutable keys.~=
operator appends a new dstring to the values of the associate dynamic array.toLower
, join
and chomp
are string functions that D allows the use of with a method syntax. The name of such functions is often very similar to Python string methods. The toLower
converts a string to lower case, join(' ')
joins an array of strings into a single string using a single space as separator, and chomp
removes a newline from the end of the string if one is present.sort
is an std.algorithm function that sorts the array in place, creating a unique signature for words that are anagrams of each other. The release()
method on the return value of sort()
is handy to keep the code as a single expression.foreach
iterates on the values of the associative array, it's able to infer the type of words
.key
is assigned to an immutable variable, its type is inferred.dchar[]
is used instead of normal UTF-8char[]
otherwise sort()
refuses to sort it. There are more efficient ways to write this program that use just UTF-8.Wikibooks has a book on the topic of: A Beginner's Guide to D |
Wikibooks has a book on the topic of: D Programming |