Tag “D”

My enthusiasm in learning D is contagious. Some of my colleagues ask me from time to time about useful resources. Here is a list of ones.

  • APT repository for D—if you are an Ubuntu fan (like me), you don’t need an explanation what is it. Here you can get latest stable DMD compiler and some useful libraries and tools.
  • DUB—it is a build tool with support of managing dependencies. Its features are similar to Maven for Java and Pip for Python.
  • Derelict—awesome collection of binding to popular C libraries. Useful in game development.
  • Vibe.d—the web framework, nuff said. Frankly, I haven’t spend a lot of time fiddling with it, but it looks promising.
  • Phobos—the standard library. It is not so diversified as Python one, but it is powerful enough. By the way, if you dream of reinventing the weel, it is your chance! There is still a lot of work.
  • The D Programming Language by Andrei Alexandrescu—the book you must read.

That is all for now. Wish you happy hacking!

I was fiddling with D during my vacation, when I had a rest from my house building. So I am going to write about the most exciting (from my point of view) feature of this programming language—the uniform function call syntax.

As it goes, an example is the best explanation.

import std.stdio;

int twice(int i) {
    return i * 2;
}

void main() {
    assert(twice(10) == 20);
}

There is nothing interesting. Code is executed as expected. But let’s make some changes.

import std.stdio;

int twice(int i) {
    return i * 2;
}

void main() {
    assert(10.twice == 20);  // Does it work?
}

Oh heck, that works! Yes, that is exactly what you think. You can call any non-member function like a member one of some type (generic or user-defined, never mind), if this function accepts argument of this type as a first parameter. In other words, you can write obj.func(arg1, arg2) instead of func(obj, arg1, arg2). In addition, you can omit parens, if there are no other arguments.

The first benefit of the feature is chaining:

import std.array;
import std.algorithm;

void main() {
    auto arr = [1, 2, 3, 4, 5]
        .filter!("a % 2")
        .map!("a * 2")
        .array();
    assert(arr == [2, 6, 10]);
}

dQuery is waiting for its heroes :)

The second one is some sort of monkey patching. However, you cannot totally change third-party class behavior. Because your non-member functions have no access to the private and protected members. But you can extend it.

And the last but not least, it significantly improves code readability.

import std.file;
import std.json;

void configure(string configPath) {
    // auto config = parseJSON(readText(configPath));  Never again!
    auto config = configPath.readText().parseJSON();
    // Do something useful...
}

P.S. Even though this feature had been mentioned in Andrei Alexandrescu’s book, “The D Programming Language” it was working for arrays only for a long time. But now it works for any type. I have checked it in DMD v2.062 compiler.