The D Language: A sweet-spot between Python and C

The D Language: A sweetspot between Python and C

Python has been one of my favorite languages since I started contributing to the Mercurial project. In fact Mercurial being in Python instead of git’s C/Bash codebase was an incentive to start working on Mercurial. I admired it’s clean syntax, it’s functional patterns, including laziness through generators and it’s ease to use. Python is very expressive and thanks to battery-included, it’s very powerful right from the beginning. I wanted to write everything in Python, but I reached limitations. CPU intensive code couldn’t be easily parallelized, it’s general performance was limiting and you had to either ship your own python or rely on system python.

For deep system integration into libraries, performance and static binaries I’ve still relied on C. One might argue that I should have used C++ over C but I really loved C’s simplicity. All it’s syntax could fit in my brain and while it’s not typesafe (see void pointer), it’s incredible fast and you can choose from the largest pool of libraries available.

So while C gave me the power and performance, Python gave me memory safety, expressiveness and not to worry about types. I’ve always looked for a middle-ground, something fast, something I can easily hook into C (before there was cython) and something expressive. Last year I found D and were lucky enough tobe able to be allowed to implement a Fuse filesystem in D and an Open Source library around it: dfuse.

For once it feels I found a language that hit’s a sweet-spot between expressiveness, close to the system, and performance. There are drawbacks, for sure. I’ll talk about that in another post.

What is D? D is a programming language developer by Walter Bright and Andrei Alexandrescu over the last 10 years. It’s newest incarnation is D2 which has been developed since 2007 and available since 2010. Both have worked extensively with C++ in the past and incorporated good parts as well as redesigning the bad parts. The current version is 2.066.1. Multiple compilers are available, the reference implementation DMD, a GCC frontend called GDC and an LLVM frontend named LDC. In addition a runtime is distributed which provides thread handling, garbage collection, etc. A standard library is available, called phobos and usually distributed together with the druntime (in fact druntime is compiled into the standard library).

What makes D beautiful? First of all, D is statically typed, but in most cases types are inferred through `auto` (which C++ borrowed later on). Secondly, D has excellent support for interacting with C and C++ (just recently namespace resolution for C++ symbols was added). But most important, D offers a variety of high level data structures as part of the language. This allows for fast and efficient prototyping. Solutions in D tend to be a nice mixture between a “python”-style solution using standard datatypes and a “C”-style approach of using specialized datastructures when needed.

In addition, D offers a very elegant template syntax to provide powerful abstractions. For example the following code generates a highly optimized version of the filter for every lamda you pass in (unlike C where we would need a pointer dereference):

But my arguable most loved feature is the unified function call syntax. Think about a structure that has a set of functions:

but you want to add a new method `drop`, however you can’t modify the original code. UCFS allows you to use the dot-notation to indicate the first argument:

This gives a powerful way to add abstractions on top of existing libraries without having to obtain or fork the source code.

Get Started

If you are interested in giving D a try, a few useful resources:

Compiler: The DMD compiler is the reference compiler. It does not produce as efficient code as GDC but usually supports the most recent features. You can obtain it from http://dlang.org.

Introductions and Books: I really like ‘The D Programming Language‘ by Andrei Alexandrescu. Sadly the book must be purchased but it’s worth it. http://ddili.org/ders/d.en/ is an excellent tutorial by Ali Çehreli.

Help: The D community is very approchable and helpful. Just write a post on the D forums or Mailinglist: http://forum.dlang.org/

Hacking: Want to hack on D or it’s standard library Phobos? It’s all on github: https://github.com/D-Programming-Language.

This is just a very short overview why I start loving D, I’ll go into more details about drawbacks, C interaction and using the standard library in the next few weeks. More D to come.

Posted January 22nd, 2015 in D, Open Source, Programming.

7 comments:

  1. greg:

    Hi;

    Did you try golang ? it seems near D2 ; I don’t know D2, but actually, I like pretty much go ;)

  2. bz:

    you’ve missed `GDC` in this sentence: The current version is 2.066.1. Multiple compilers are available, the reference implementation DMD, a GCC frontend called and an LLVM frontend named LDC`

  3. Hassan:

    You don’t have to explicitly specify “auto” in the foreach loops, a simple foreach(e; arr) would suffice. In fact, I couldn’t get it to compile in v2.066.1 unless I removed it (and replaced auto result with typeof(return) result in the second snippet), which is strange because I thought this was possible to do in some previous releases. Maybe they removed it ?

  4. cym13:

    Hey, you’re post is on

  5. cym13:

    EDIT: I meant reddit: http://www.reddit.com/r/programming/comments/30qqck/the_d_language_a_sweetspot_between_python_and_c/

  6. Cody Casterline:

    FYI:

    1) you don’t need to specify auto in `foreach`es.
    2) there’s a filter function in std.algorithm that works just like yours, but returns a (lazy?) Range object. :)


    import std.stdio;
    import std.algorithm: filter;

    void main()
    {
    auto list = [1, 2, 3, 4, 5, 6];
    foreach (item; list.filter!(x => x % 2 == 0))
    {
    writeln(item);
    }
    }

  7. Debamitro Chakraborti:

    I so very much agree. I have recently started trying out D although I’ve been reading about it for a while. It is a language which encourages better programming and yet is very easy to pick up if you know C or C++ or Python.

Leave a response: