The D Programming Language: Getting started.

NardCake 1 Tallied Votes 797 Views Share

This tutorial is designed to get someone started with D that hasn't used it yet, it assumes you have some type of programming experience.

What is D?

In one sentence (from dlang.org) "D is a language with C-like syntax and static typing. It pragmatically combines efficiency, control, and modeling power, with safety and programmer productivity."

You may not know what this means, you will soon see throughout this tutorial. In my opinion D is a beautiful, powerful, and convienient language. It combines the low level features of C/C++ and high level features that can be seen in Java. All of that combined with object oriented paradigms, great meta programming and more.
There is a list of features on the website.

Getting a D compiler

If you don't already know, D is a compiled systems programming language, so you're going to need a compiler. There are plenty of options available including DMD (Digital Mars D Compiler, it's the reference compiler), GDC (GNU D Compiler), and LDC (LLVM D Compiler). All of these use the open source D front end by Digital Mars. I personally use DMD because I like it the most, and it's the reference compiler so it's always up to date. You could use GDC as it's very mature (of my understanding) but I would hesitate before using LDC due to the fact it's not as up to date or bug free as others (again, what I've heard).

Downloads

  • DMD Make sure you grab DMD2 (top table) and the correct one for your system, I trust that you know how to install it yourself.
  • GDC
  • LDC

After installation it's best to test that it's installed properly so open your shell and just type the name of the compiler you installed, for example dmd and if you get a help page or an error from the compiler you know it's been installed properly.

Hello, World!

That's right, the infamous "Hello, World!" is making it's way into this tutorial. As basic of a program this is the hello world program is one of the most important programs you can write in a language in my opinion, because it's the first one you write, it's simple, gives an introduction to how things work, basic syntax, and to test the compiler.
If you've written any C-style language D probably won't be any difficult for you, just got to learn a few things about the language. If you haven't written in a C-style language before I'm afraid you have some work to do!
Anyway, here's the "Hello, World!" program:

import std.stdio;
void main(string[] args)
{
    writeln("Hello, World!");
}

Now you can compile it like this (I'm using DMD, also assuming you named it hello.d): dmd hello.d
This compiles hello.d.
So now, I'm simply going to walk you through what's happening here.
So first thing we do here is import standard I/O from the D standard library which is code named, Phobos. The equivalent of this in C might be: #include <stdio.h>.
Next we have the entry point to our program, the main function. If you've written C/C++ you're most likely very familiar with this except you probably had it with a return type of int and had it return 0; for example. If you haven't noticed it takes the program arguments as an array of strings--that's correct a string is a native type in D.
After that we finally write "Hello, World!" on screen! With the writeln function from the stdio library. This would be the equivalent of printf("Hello, World!\n"); in C. Other functions like this are also available such as write, writef, and writefln. The 'f' means formatted similar to printf, try them out! (Or check out the Phobos docs)
Just so you C programmers understand, here is this program written in C:

#include <stdio.h>
int main()
{
    printf("Hello, World!\n");
    return 0;
}

Another thing to mention now is that D can link to C code due to it's compilation it is C compatible! Meaning you can write wrappers to C libraries and such in D. In the D runtime there are bindings to the C stdio and stdlib libraries, here's an example with printf:

import std.c.stdio;
void main()
{
    printf("Hello, World!");
}

Another example

This example will simply use Phobos to read a file specified by a program arguments, enclosed in a try-catch statement to demonstrate exceptions.

import std.stdio, std.file;
void main(string[] args)
{
    try {
        writeln(std.file.read(args[1]));
    } catch(Exception e) {
        writeln(e.msg);
    }
}

Note: I used std.file.read to avoid name conflicts with std.stdio.
This works, but not very safe or user friendly, so here's a better version:

import std.stdio, std.file;
void main(string[] args)
{
    if (exists(args[1]) && isFile(args[1])) {
        writeln(std.file.read(args[1]));
    } else {
        writefln("The file '%s' doesn't exist.", args[1]);
    }
}

Those were just a few examples to get you started.

The End...

Hopefully this got you started with D if you were interested. I obviously didn't go over everything (Not even OOP features!) and you are encouraged to do some research yourself to find out more about D. I really do believe it could replace C++ and is a fantastic langnauge. Hopefully I supplied you with all of the proper links through out the text and good luck!

NardCake 30 Posting Pro in Training

Also if you're interested in some serious development you may be interested in the dub package manager and build tool!
Download

Tarek_2 26 Light Poster

Interesting, I see the java famous "import" and "string" is a data type, the main method is almost the java one, but without creating a class to test few lines of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.