Getting started with D

Timur Gafarov
4 min readMar 1, 2020

--

Contrary to common belief, I think that D is a perfect first language to learn by a complete beginner of native programming. D has its roots in C/C++ realm, but its syntax is clean, modern and friendly. D doesn’t require deep knowledge of computer architecture to start writing programs. One of the strongest parts of D is in that it’s equally fit for both low level (C-like) and high level (scripting-like) programming.

Many beginners however find it easier to start with C++ because of readily available tools and IDEs like Visual Studio. D doesn’t have a standard IDE, and this can be a barrier for newcomers who just want to create a simple “Hello, World” project. So I wrote this simple five step guide which should help them.

Step 1. Install the compiler

Head to https://dlang.org/download.html and get the latest version of the reference compiler, DMD. There is also LDC, also very good D toolchain, but it can be a bit messier to install. DMD comes with an installer app that does everything for you.

Step 2. Create a new project

Create an empty directory for your project, open up the terminal window and type the following:

dub init

DUB is a standard package manager and build system for D. Nowadays you don’t have to call the compiler directly, it is highly recommended to work with DUB. It already comes with the compiler distribution, so you don’t have to install it separately.

“dub init” will initialize a template project, prompting you for a minimal information about it. You can leave package recipe format as a default option (json). Then type in a name of your project, description, author’s name, license, copyright and a list of dependencies. All of these fields are actually optional, so don’t worry if you are not sure about them, you can specify them later by editing dub.json.

Now do

dub build

And DUB will compile your application. If you run it in terminal, you should see

Edit source/app.d to start your project.

Which points us to the next step, configuring an editor to work with the source code.

Step 3. Install Atom and ide-dlang

My editor of choice for D is Atom. Why? Because it is minimalistic, beautiful, customizable and extendable. It requires a lot less initial configuration for a comfortable work that any other editor I’ve seen. There is an excellent D integration plugin for Atom, ide-dlang.

Now, with Atom and ide-dlang installed, open your project:

File → Open Project Folder

…and you are ready to edit your source/app.d:

import std.stdio;void main()
{
writeln("Edit source/app.d to start your project.");
}

Step 4. Create a build configuration

If you don’t like switching between editor and terminal window, you can install build package to compile your project directly from Atom. Create an .atom-build.jsonfile in the project directory with the following content:

{
"cmd": "dub",
"args": ["run"],
"cwd": "{PROJECT_PATH}"
}

Now you can build and run the project by pressing F9. The output will be forwarded to Atom’s built-in console.

Atom with D tools

Step 5. Add dependencies

As I mentioned earlier, DUB is a package manager. It has a registry with hundreds of libraries and programs that you can install with one command, dub add :

dub add dlib

dlib is a collection of general-purpose code for such tasks as image processing and geometric transformations. In the code below I used it to load a PNG image and print its width:

import std.stdio;
import dlib;
void main()
{
auto img = loadPNG("image.png");
writeln(img.width);
}
Decoding PNG with dlib

What next?

--

--

Timur Gafarov
Timur Gafarov

No responses yet