September 11, 2020

Introduction to D Programming Language

Content

D is a general-purpose programming language that combines a wide range of powerful programming concepts. It was developed to read/write/run the code fast. It has a variety of features like -

  • static typing

  • blazingly fast compile-times

  • memory-safe subset (SafeD)

  • multi-paradigm (imperative, structured, object oriented, generic, functional programming purity, and even assembly)

  • high performance, compiled language

  • high level constructs for great modeling power

  • compatible with C application binary interface

  • and many more…

Installation

D can be downloaded from its website
It has plugins for many editor tools like Atom, Sublime Text and VS Code.

D comes with a compiler dmd. By using it we can run any D program file. D program file has file extension .d
Below is the syntax to run D program -

dmd hello.d

Hello World

Let’s start with a hello world program.

1
2
3
4
5
import std.stdio;

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

First line is used to import the libraries required for our program. Here we used std.studio which is used to handle Data Input and Output.
Note: In D language we used Turtles all the way down philosophy. That means we can put import inside the function or any other scope. It doesn’t need to be on top of the source file. So it’ll still be valid if we write like this:

1
2
3
4
void main() {
  import std.stdio;
  writeln("Hello World!");
}

If we want to import selective libraries from a module, we can do it using : like this:

import std.stdio : writeln, writefln;

In D, program starts its execution from the main method. Before defining a method, we define return type. In this case it is void that means it’ll not return anything.

Data Types

D supports a variety of data types. Following is the list of some of them and their size:

Data TypeSizeDefault Value
bool8-bitfalse
byte, ubyte8-bit0
short, ushort16-bit0
int, uint32-bit0
long, ulong64-bit0L
float32-bitfloat.nan
double64-bitdouble.nan
real>= 64-bit (generally 64-bit, but 80-bit on Intel x86 32-bit)real.nan

D types have certain properties like min, max which can be accessed using a dot after their name. For e.g.

.min will give minimum value of that data type. .init will give ‘initial value’.

A value can be assigned to a variable using = operator.

1
2
3
4
5
6
7
8
import std.stdio: writeln;

void main() {

  int b = 100;
  writeln("Value of b is ", b);
  writeln(int.min, " ", int.max);
}

We don’t need to always declare type on both sides of the = operator. In order to make our code more simple and less redundant, we can use a keyword auto. It helps the compiler to understand the datatype based on the expression on the right-side of assignment operator.

auto duration = 619;

Logical Expression

We can use boolean expressions in D for boolean arithmetic(two possible outcomes true or false). D supports various logical operators:

== for equality check, != not equal to, || Logical OR, && Logical AND ^ Logical XOR. Let’s see them in action:

1
2
3
4
5
6
7
8
9
10
import std.stdio;

int main() {
  bool a = false;
  bool b = true;

  writeln("OR value: ", a || b); // true
  writeln("AND value: ", a && b); // false
  return 0;
}

Conditional Statements and Loops

D provides conditional statements and loops to control the flow of our program. We can provide logical expressions to decide whether our program will run that logic part or not.

For example, we can if condition to execute a block based on some condition being true. Syntax is like this:

1
2
3
if (logical_expression) {
  // logic part
}

if the condition is false then we can put that logic in else part.

1
2
3
4
5
6
7
8
9
void main() {
  bool hunger = true;

  if (hunger) {
    writeln("Eat Pizza");
  } else {
    writeln("Go to sleep");
  }
}

We can chain multiple if-else statements for multiple conditions.

Let’s try some loops in D. Loops are used for repititive tasks. For e.g. we want to perform some calculations 5 times, instead of writing it 5 times, we can use a loop. The most common loops are while and for.

while will execute till the given condition doesn’t hold false.

1
2
3
4
bool condition = true;
while (condition) {
  writeln("Someone stop this!!!");
}

This example will run infinitly. Because the condition is not getting updated.

break statement can be used to break the flow of the loop.

for loop has three sections - initialization, condition check, iteration(can be increment/decrement or else).

1
2
3
for (int i = 1; i < 10; i++) {
  writeln(i);
}

This loop will run 9 times.

Similar to while, we can also run an infinite for loop.

1
2
3
for (;;) {
  // ...
}

Conclusion

That’s all for now. Though this was a baby step towards learning this new programming language, but fundamentals are always good to revise. Now it’s your turn to try D and make some awesome stuff. Happy Coding :)

Group 9
Group 9