Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News An Introduction to D and Visual D

An Introduction to D and Visual D

D is a systems programming language from Digital Mars that focuses on “combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python.” While still being a statically typed language that compiles directly to native code, the syntax looks very much like Java or C# but it has some interesting advances.

Once such advance is explicit support for lazy arguments. When a parameter is tagged with the keyword “lazy” it indicates that the associated argument will be evaluated 0 or more times. This differs from languages such as Haskell which lazily evaluate arguments 0 or 1 times, but never more.

To assist in program correctness, especially in the realm of concurrency and multithreading, D supports both “immutable” and “const” as first-class concepts. Unlike C++, the immutable and const modifiers in D are transitive. If a variable is tagged with immutable, neither it nor anything you can access via it can be modified. The const keyword works the same way, though a given object may still be modified via a non-const reference.

D also combines many of the popular features from other languages. For example it has for-each loops, switches on strings, array slicing, closures, try-catch-finally blocks, and the separation between value types and reference types. At the same time it allows for direct memory access, 80-bit floats, struct member alignment control, and inline assembly.

Usually switching to native code comes with a high price in terms of safety. While C and C++ are the most well-known, most native languages are susceptible to coding errors such as buffer overflows. D prevents these by a combination of compile time and runtime array boundary checking. Likewise the contents of non-zero length arrays are auto-initialized to their default values.

In terms of memory management D is very much like .NET. By default reference types are stored in a garbage-collected heap and value types are stored either on the stack or inside other objects. D can also stack-allocate reference types so long as a pointer to them doesn’t escape. And like .NET you can also take pointers to unmanaged memory. Keeping these separate is important, as many of the operations that you can perform on an unmanaged pointer simply aren’t safe on a managed one.

For developers with access to a non-Express Visual Studio, there is a project called Visual D that offers Visual Studio plugins. Lloyd has an article showing how to setup your environment with Visual D.

There are currently four implementations of the D programming language. Digital Mars offers free compilers for Windows, Linux, OS X, and FreeBSD. There is also the Gnu D compiler, built on gcc. LLVM has a beta of their D compiler available, and finally there is the partial implementation for .NET.

Rate this Article