The D programming language is a statically typed, originally compiled language that has some essential features inspired by Python.
Arrays and associative arrays are built into the language. There are no concepts on lists, but the std.range and std.algorithm libraries fill in most of this void. For example, here you can summarize all even numbers from 0 to 100 in D:
auto result = reduce!"a + b"( filter!"a % 2 == 0"( iota(0, 100) ) );
There are still no arguments with keywords, but closed. Tuples are supported, but not unpacked automatically.
In D, you avoid specifying classes (and types in general) everywhere with the auto keyword and with templates. For example, here is a generic code to find the product of an array of any number type:
// The return type of product() is inferred. auto product(T)(T[] array) { T ret = 1; foreach(num; array) { // typeof(num) is inferred. ret *= num; } return ret; }
Metaprogramming Support
D consists of an introspection of compilation time (for example, you can iterate over fields of a class or structure at compile time), information about the type of runtime, and patterns that are actually designed for metaprogramming outside of simple generics. For example, here's how to write a generic function that generates a default comparison operation for two structures, which is useful if you need arbitrary full ordering for something like a binary tree:
int compareStructs(T)(T lhs, T rhs) { foreach(tupleIndex, value; lhs.tupleof) { if(value < rhs.tupeof[tupleIndex]) { return -1; } else if(value > rhs.tupleof[tupleIndex]) { return 1; } } return 0; }
dsimcha Feb 15 '10 at 19:39 2010-02-15 19:39
source share