D Programming Language Tutorial
Ali Çehreli



İngilizce Kaynaklar

Diğer



The Hello World Program

The first program to show in most programming language books is the hello world program. This very short and simple program merely writes "hello world" and finishes. This program is important because it includes some of the essential concepts of that language.

Here is a hello world program in D:

import std.stdio;

void main()
{
    writeln("Hello world!");
}

The source code above needs to be compiled by a D compiler to produce an executable program.

Compiler installation

At the time of writing this chapter, there are three D compilers to choose from: dmd, the Digital Mars compiler; gdc, the D compiler of GCC; and ldc, the D compiler that targets the LLVM compiler infrastructure.

dmd is the D compiler that has been used during the design and development of the language over the years. All of the examples in this book have been tested with dmd. For that reason, it would be the easiest for you to start with dmd and try other compilers only if you have a specific need to. Note, as of this writing, dmd version 2.066.1 was the latest version tested.

To install the latest version of dmd, go to the download page at Digital Mars and select the compiler build that matches your computer environment. You must select the dmd build that is for your operating system and package management system, and whether you have a 32-bit or a 64-bit CPU and operating system. Do not install a D1 compiler. This book covers only D version two.

The installation steps are different on different environments but it should be as easy as following simple on-screen instructions and clicking a couple of buttons.

Source file

The file that the programmer writes for the D compiler to compile is called the source file. Since D is usually used as a compiled language, the source file itself is not an executable program. The source file must be converted to an executable program by the compiler.

As with any file, the source file must have a name. Although the name can be anything that is legal on the file system, it is customary to use the .d file extension for D source files because development environments, programming tools, and programmers all expect this to be the case. For example, test.d, game.d, invoice.d, etc. are appropriate D source file names.

Compiling the hello world program

Copy or type the hello world program above into a text file and save it under the name hello.d.

The compiler will soon check that the syntax of this source code is correct (i.e. it is valid according to the language rules) and make a program out of it by translating it into machine code. To compile the program follow these steps:

  1. Open a console window.
  2. Go to the directory where you saved hello.d.
  3. Enter the following command. (Do not type the $ character; it is there to indicate the command line prompt.)
$ dmd hello.d

If you did not make any mistake, you may think that nothing has happened. To the contrary, it means that everything went well. There should be an executable file named hello (or hello.exe under Windows) that has just been created by the compiler.

If the compiler has instead printed some messages, you probably have made a mistake when copying the program code. Try to identify the mistake, correct it, and retry compiling. You will routinely make many mistakes when programming, so the process of correcting and compiling will become familiar to you.

Once the program has been created successfully, type the name of the executable program to run it. You should see that the program prints "Hello world!":

$ ./hello     ← running the program
Hello world!  ← the message that it prints

Congratulations! Your first D program works as expected.

Compiler switches

The compiler has many command line switches that are used for influencing how it compiles the program. To see a list of compiler switches enter only the name of the compiler:

$ dmd    ← enter only the name
DMD64 D Compiler v2.066.1
Copyright (c) 1999-2014 by Digital Mars written by Walter Bright
Documentation: http://dlang.org/
Usage:
  dmd files.d ... { -switch }

  files.d        D source files
...
  -de            show use of deprecated features as errors (halt compilation)
...
  -unittest      compile in unit tests
...
  -w             warnings as errors (compilation will halt)
...

The abbreviated output above shows only the command line switches that I recommend that you always use. Although it makes no difference with the hello world program in this chapter, the following command line would compile the program by enabling unit tests and not allowing any warnings or deprecated features. We will see these and other switches in more detail in later chapters:

$ dmd hello.d -de -w -unittest

The complete list of dmd command line switches can be found in the DMD Compiler documentation.

One other command line switch that you may find useful is -run. It compiles the source code, produces the executable program, and runs it with a single command:

$ dmd -run hello.d -w -unittest
Hello world!  ← the program is automatically executed
IDE

In addition to the compiler, you may also consider installing an IDE (integrated development environment). IDEs are designed to make program development easier by simplifying the steps of writing, compiling, and debugging.

If you do install an IDE, compiling and running the program will be as simple as pressing a key or clicking a button on the IDE. I still recommend that you familiarize yourself with compiling programs manually in a console window.

If you decide to install an IDE, go to the IDEs page at dlang.org to see a list of available IDEs.

Contents of the hello world program

Here is a quick list of the many D concepts that have appeared in this short program:

Core feature: Every language defines its syntax, fundamental types, keywords, rules, etc. All of these make the core features of that language. The parentheses, semicolons, and words like main and void are all placed according to the rules of D. These are similar to the rules of English: subject, verb, punctuation, sentence structure, etc.

Keyword: Special words that are a part of the core features of the language are keywords. Such words are reserved for the language itself, and cannot be used for any other purpose in a D program. There are two keywords in this program: import, which is used to introduce a module to the program; and void, which here means "not returning anything".

Library and function: The core features define only the structure of the language. They are used for defining functions and user types, and those in turn are used for building libraries. Libraries are collections of reusable program parts that get linked with your programs to help them achieve their purposes.

writeln above is a function in D's standard library. It is used for printing a line of text, as its name suggests: write line.

Module: Library contents are grouped by types of tasks that they intend to help with. Such a group is called a module. The only module that this program uses is std.stdio, which handles data input and output.

Character and string: Expressions like "Hello world!" are called strings, and the elements of strings are called characters. The only string in this program contains characters 'H', 'e', '!', and others.

Order of operations: Programs complete their tasks by executing operations in a certain order. These tasks start with the operations that are written in the function named main. The only operation in this program writes "Hello world!".

Significance of uppercase and lowercase letters: You can choose to type any character inside strings, but you must type the other characters exactly as they appear in the program. This is because lowercase vs. uppercase is significant in D programs. For example, writeln and Writeln are two different names.

We will cover all of the features of D in more detail in the following chapters.

Exercises
  1. Make the program output something else.
  2. Change the program to output more than one line. You can do this by adding one more writeln line to the program.
  3. Try to compile the program after making other changes; e.g. remove the semicolon at the end of the line with writeln and observe a compilation error.