This post begins a series of translations of the
D Programming Language Tutorial , in order to compensate for the information vacuum about this system language. Each part will contain a constant amount of material from the book, since the original chapters have a wide variation in size: from a couple of paragraphs to several printed pages. All code samples are checked on the current
release of the dmd compiler 2.065 , and if there are problems with the technical part, please unsubscribe in the comments.
Other parts:
')
Thanks
I would like to thank my wife and daughter who survived so many long hours as I wrote the original version in Turkish and also the English translation of this book.
The chapters have been initially tested by members of the Turkish
Ddili Forum . I am grateful to the Turkish community D for maintaining my excitement and motivation at a high level.
Mert Ataol, Zafer Ăelenk, and Salih Dinçer wrote reviews on almost every line of the book. Can Alpay Ăiftçi and Faruk Erdem Ăncel played an important role in the development of the book and ddili.org.
Thanks to the following people for their significant corrections, recommendations and ideas: Ergin GĂźney, Jordi Sayol, David Herberth, Andre Tampubolon, Gour-Gadadhara Dasa, RaphaĂŤl Jakse, Andrej Mitrovic, Johannes Pfau, Jerome Sniatecki, Jason Adams, Ali H. ĂalÄąĹkan, Jurczak, Brian Rogoff, Michael Strashun, Joseph Rushton Wakeling, Tove, Hugo Florentino, Satya Pothamsetti, LuĂs Marques, Christoph Wendler.
This book was read by Ergin GĂźney to improve my English (original Inglish) to normal English.
Introduction
The book is intended to teach the language D readers - beginners in the field of programming. Although having experience in other programming languages ââwould undoubtedly be helpful, this book begins with the basics. If you are interested in learning how to program, I hope that you will find the book useful.
In order for the book not to be useless, we will need an environment for writing, compiling, and running programs on D. This
development environment must include at least the following elements:
- text editor
- compiler for D
Instead of downloading and installing them separately, you can consider the option with an
integrated development environment (IDE). You can find information about editors and IDE for D on the
Editors and
IDE pages on dlang.org. It is impossible to learn how to program in D (approx. Yes in general in any language other than pseudocode) without a text editor or compiler. The instructions for installing the
dmd compiler and how to use it will be discussed later in the next chapters.
Each chapter of the book attempts to introduce as few new concepts as possible. Most chapters contain several exercises, and there are solutions to them in order to compare their solutions with mine. (Note. In this translation, the solutions are under the spoilers next to the tasks.)
I assume that you do not miss a chapter. If you come across chapters that are particularly hard given, this may be due to the fact that the book fails to provide all the necessary concepts. Please write to the author (acehreli@yahoo.com) (note leave a comment) about such issues to help make this book more useful.
The book does not reveal programming using the
graphical user interface (GUI). Although many programs are much more convenient to use with the GUI, the GUI does not directly relate to programming languages. Moreover, design decisions and GUI programming style can conflict with the style of the language itself and its standard library, and complicate language learning. Therefore, the book describes only the
console program. Once you have learned the basics of D and its standard library, Phobos, you can use any library for the GUI you want.
Book chapters are available online as they are translated from Turkish. You can use
the RSS feed to keep abreast of new chapters.
Studying programming is much more fun in a team. Visit
D.learn newsgroup to follow the discussions and ask questions and answer them.
Programming practice
It is very difficult to determine what is indisputable what exactly the programming practice includes, but the craft aspect in it is very strong. Some conclusions about programming:
- This is the task of creating programs that make the computer behave in the expected way.
- Since it requires tools and the use of these tools is guided by the experience of programmers-masters, it is a craft
- Since it involves solving problems with constraints, it is an engineering skill.
- It is very exciting and satisfying.
- It is not a visual art, but as far as possible in any human activity, programs may be works of art.
- It is not a science, but the methods used in it are created by science-informatics.
It can be very difficult to learn programming and teach it.
Programming has been taught since the 1950s, and effective or successful teaching methods have not yet been developed.
Unfortunately, learning to program can be a daunting task for about half of all students. According to a
scientific article (note original. Link is not working), those who study programming with ease are those who are able to create
consistency models to describe new situations they encounter.
Some programming difficulties are caused by the number of technical details that need to be learned.
The hello world
The first program in most books on programming is the
hello world program. This is a very short and simple program that displays "hello world" and ends. This program is important because it includes some basic language concepts.
Below hello world on D:
import std. stdio ;
void main ( )
{
writeln ( "Hello world!" ) ;
}
The source code above must be compiled by the D compiler to create the file being called.
Compiler installation
At the time of this writing, you can choose from three D compilers:
dmd , Digital Mars compiler;
gdc , D compiler for GCC; and
ldc , a compiler that uses the
LLVM infrastructure.
The dmd - D compiler that has been used to design and develop a language for many years. All examples in this book have been tested on
dmd . For this reason, the easiest way out is to start with
dmd and try other compilers if there is a specific need for this (note, for example,
gdc produces the most optimized code).
To install the latest version of
dmd, go to
the download page on Digital Mars and select the version of the compiler that suits your computerâs environment. You must choose the version of
dmd that matches the installed operating system and package manager, and matches the processor architecture: 32-bit or 64-bit. Do not install the compiler for D1 (note. The first version is history)! This book covers only D of the
second version .
The installation sequence is different for different environments, but it should be as simple as following simple instructions on the screen and pressing a couple of buttons.
Source code
The file that the programmer writes for compilation is called the source file or just the source code. Since D is a compiled language, the sources themselves are not executable files. The source code must be translated into an executable program by the compiler.
Like all files, the source must have a name. Although this name can be anyone that is allowed by the operating system, the
.d extension is usually used for D source files, as development environments, programming tools, and programmers expect such an extension. For example,
test.d ,
game.d ,
invoice.d , etc. suitable as names for the source.
Compiling hello world
Copy the program text above to a text file and save it as
hello.d .
The compiler will soon check the correctness of the source code syntax (i.e., compliance with the rules of the language) and create a program from it by translating it into machine codes. To compile, follow these steps:
- Open console window
- Change to the directory where hello.d is saved
- Enter the following command (Do not write the $ symbol, it is to denote the command line.)
$ dmd hello.d
If you have not made mistakes, it may seem that nothing happened. Otherwise, it means that everything went well. An executable file called
hello (or
hello.exe under Windows), which has just been created by the compiler, should appear in the folder.
If instead the compiler output several messages, you may have made a mistake when copying the program code. Try to find the error, fix it and restart the compilation. You will make many mistakes in programming, so the process of correcting and compiling them will become familiar.
Once the program has been successfully created, write the name of the executable file to run it. The program should output âHello world!â:
$ ./hello â Hello world! â ,
(note under Windows instead of ./hello you need to enter hello)
Congratulations! Your first D program works as expected.
Compiler flags
The compiler has many command line options that are used to influence the compilation process. To see the list of parameters, enter only the compiler name:
$ dmd â DMD64 D Compiler v2.065 Copyright (c) 1999-2013 by Digital Mars written by Walter Bright Documentation: http://www.dlang.org/index.html Usage: dmd files.d ... { -switch } files.d D source files ... -unittest compile in unit tests ... -w enable warnings ...
The abbreviated output above shows only flags, which I recommend to always use. Although it does not matter for the hello world program in this chapter, the following command will compile the program with warnings and
unit tests included. We will look at these and other parameters in more detail in the following chapters:
$ dmd hello.d -w -unittest
A complete list of
dmd parameters can be found in the
official DMD documentation .
Another flag that may be useful:
-run . It compiles the source code, creates an executable file, and runs it in one command:
$ dmd -run hello.d -w -unittest Hello world! â
IDE
In addition to the compiler, you can install an IDE (integrated development environment). IDEs are designed to simplify software development by simplifying the steps of writing, compiling, and debugging code.
If you install an IDE, the program will be compiled and run simply by pressing the keyboard key or the button in the IDE. I still recommend reading the manual compilation of programs in the console window.
If you decide to install an IDE, go to the
IDE page on dlang.org to see the list of available IDEs (note, I use DDT).
Parsing hello world programs
Here is a brief list of the many concepts from D that appeared in this short program:
Kernel: Each language defines its own syntax, fundamental types, keywords, rules, etc. They all form the
core of this language. Parentheses, semicolons and words such as:
main and
void are all in accordance with rules D. This is similar to the rules of the English (Russian) language: subject, verbs, punctuation, sentence structure, etc.
Keyword: Special words that are part of the core language are called
key . This program has two keywords:
import , which is used to connect modules to the program; and
void , which means "returns nothing."
Library and function: The kernel defines only the structure of the language. It is used to define functions and user-defined types, and they in turn are used to design libraries. A library is a collection of reusable parts of programs that
link (note linked) with your programs to help them achieve their goals.
The writeln above is a
function in the standard
library D. It is used to display a line of text, as you might guess from its name: write line - write a line.
Module: The contents of libraries are collected by the types of tasks for which they are intended. Such a group is called a module. The only module that our program uses is
std.stdio , which is responsible for entering and entering data.
Characters and strings: Expressions such as
"Hello world!"
are called
strings , and the elements of strings are called
characters . The only line in our program contains the characters
'H'
,
'e'
,
'!'
other.
Execution order: Programs perform their tasks by calling operations in a specific order. Task execution begins with operations that are written in a function called
main . The only operation in our program displays the string
"Hello world!"
.
Case Importance: You can select any characters within strings, but you must use the rest of the characters exactly as they appear in the program. This is so, as in the D programs the register is important. For example,
writeln and
Writeln are two different names.
We will look at all of these D features in more detail in the following chapters.
Exercises
- Write a program that displays something else.
- Modify the program to display more than one line.
- Try to compile the program after other changes: for example, remove the semicolon at the end of the line with writeln and examine the compilation error.
Solutionsimport std. stdio ;
void main ( )
{
writeln ( "Something else ...: p" ) ;
}
import std. stdio ;
void main ( )
{
writeln ( "A line ..." ) ;
writeln ( "Another line ..." ) ;
}
- The following program cannot be compiled because there is a semicolon at the end of the line with writeln :
import std. stdio ;
void main ( )
{
writeln ( "Hello world!" )
}
writeln and write
In the previous chapter, we saw that
writeln takes a string inside parentheses and prints it.
The parts of the program that actually do the work are called
functions, and the information they need to do the work is called
parameters . The act of passing such information to a function is called
passing parameter values to a function. Parameters are passed to the function between the parentheses and are separated by commas.
Note: The word parameter describes the information that is passed to the function at the conceptual level. The specific information that is actually passed during program execution is called an argument. Although wrong, these terms sometimes replace each other in the software industry.writeln can take more than one argument. It prints them sequentially, one by one, on the same line:
import std. stdio ;
void main ( )
{
writeln ( "Hello world!" , "Hello fish!" ) ;
}
Sometimes all the information at once, which should be printed on one line, can be difficult to access in
writeln . In such cases, the first part of the line can be printed with
write , and the last part with
writeln .
writeln goes to the next line,
write remains on the same:
import std. stdio ;
void main ( )
{
// Write what we can at the moment
write ( "Hello" ) ;
// ... suppose there are more operations here ...
write ( "world!" ) ;
// ... and finally:
writeln ( ) ;
}
Calling
writeln with no parameters just ends the current line.
Lines that begin with
//
are called
comment lines or just
comments . Comments are not part of the code in the sense that they do not affect the behavior of the program. Their only purpose is to explain what the code does in specific sections of the program. The audience of comments is anyone who can later read the source code, primarily including the programmer who wrote these comments himself.
Exercises
- Both programs in this chapter print lines with no spaces between them. Change the program so that there are spaces between the arguments, as in "Hello world!".
- Also try calling write with more than one parameter.
Solutions- One of the methods is to use another parameter in the middle:
writeln ( "Hello world!" , "" , "Hello fish!" ) ;
- write can also take several parameters:
write ( "one" , "two" , "three" ) ;