A Beginner's Guide to D/The Basics/Types and Math

What are types?
If you look directly at the contents of a computer's memory, you see only a lot of bytes. These are often shown as numbers but in fact they don't have a meaning on its own, not even the meaning of a number.

Types are used in computer languages like D or C to give one byte or a sequence of bytes a meaning. Types define if these bytes are a number, a text, a date or whatever.

The Basic Types
Two basic types have already been introduced: int and float, for non-decimal and decimal numbers, respectively. Here, all of the basic types are introduced, including limits on their values and how large they are in memory.

Initialization
[TODO: Should some of the following sub-sections be placed elsewhere?] You can initialize a basic variable, or set it to a default value, by assigning it a value in its declaration:

int five = 5; double twoPointFive = 2.5;

In D all basic variables have a default value that they are initialized to if the programmer does not specify one. This may seem intuitive, but some languages (most notably C) do not automatically initialize variables unless explicitly told to do so which can lead to many problems because without initialization variables contain garbage by default (as an artifact of how computer memory works). All integer number types are default initialized to 0. All decimal number types are default initialized to NaN, a special value meaning "Not a Number". Character types are default initialized to their maximum values. If you specifically want any basic variable to be initialized with garbage, initialize them with the special value void which means no initialization.

int garbage = void; //This variable contains garbage

Note: Do not count on the garbage in a void-initialized variable being random; more often than not it will be anything but.

Imaginary and Complex Numbers
If you are not familiar with the mathematical concept of imaginary and complex numbers, feel free to skip this sub-section. D has built-in support for imaginary and complex decimal number types. D's imaginary and complex number types are based on its real decimal types (float, double, or real). An arithmatic operation involving a real decimal number and an imaginary or complex number results in a complex number and all complex arithmatic works the same as in algebra. The type names of imaginary number types are the same as real decimal types except they are prefixed with an i, and the type names of complex numbers are likewise the same as real decimal types except prefixed with a c. Imaginary numbers are initialized to NaN * 1.0i, and complex numbers are initialized to NaN + NaN * 1.0i. Imaginary literals are the same as decimal literals except they are followed by an i. Complex literals are simply real literals plus imaginary literals. For example:

One footnote is that complex decimal types take up double the amount of memory as their base real and imaginary decimal types because complex types are really just a real decimal and an imaginary decimal number stored together as one variable.

User Defined Types
D also supports several user defined types including enums, <tt>struct</tt>s, <tt>class</tt>es, and more. These types are very powerful, but they are much more complicated to use. They will be covered in-depth later in the book.

(Stub)

Arrays
An array is a way to group data of the same type. An array contains any number of elements, and each element is a value of a specified type.

Arrays can be made from any type (including other array types) similarly. The type for an array of <tt>int</tt>s with five elements is: int[5]

So, a variable called 'a' which is an array of five <tt>int</tt>s is: int[5] a;

Of course, this can be generalized for any type and count: type[count]

You can also create arrays with no specified number of elements, which can then be expanded to fit any number of elements. These are called dynamic arrays, and the alternatives (with a set number of elements) are called static arrays. They are specified similarly to static arrays, but the count is excluded. A dynamic array of any number of <tt>int</tt>s is: int[]

A variable called 'a' which is a dynamic array of any number of <tt>int</tt>s is: int[] a;

And of course, this can be generalized for any type with: type[]

[TODO: Array literals and initializing dynamic arrays to them? Should this be saved for later?]

Array Access
Array elements can be accessed like normal variables in code. That is, you can use an array element nearly anywhere you could use a simple value. To access element 1 from an array named 'a', for example: a[1]

The number for the element is called the index. The range of valid indexes is from 0 to 1 less than the number of elements; that is, for an array with 5 elements, the range of valid indexes is from 0 to 4, not from 1 to 5.

Array access can of course be generalized: array[index]

And the index can be far more complex than just a number: it can be a variable, or even a mathematical expression.

Strings
In D and many other language, a series of textual characters is called a string. In some languages, there is a special type dedicated to this, but D takes it at face value: a string is just a series of characters, and a series of characters is just an array of characters. So, you can use the <tt>char[]</tt> type (that is, a dynamic array of <tt>char</tt>s) as a string.

Similar to numeric types where you could either write a variable of numeric type or directly a fixed number in an arithmetic expression in the source code and where you could assign a fixed number to a numeric variable, you can write a fixed string in the source and assign it to a <tt>char[]</tt> variable (often also called string variable).

To mark something as a fixed string (a so-called string literal) it must be enclosed in double quotation marks: "Hello, world!" A program writing this string literal to a console window (also known as a DOS-box in Windows), such as the example given in Basic Output, would give: Hello, world! Notice the lack of quotes in the output.

Escape sequences
Sometimes you want to add "special" characters to a string literal. This means
 * 1) characters not available on your keyboard
 * 2) control characters which are not visible when printed but instead are a simple command to the console to do something.
 * 3) characters with particular meaning like the double quotation mark.

The most commonly used of the second category is the newline character which ends one line on a console and starts a new one.

To include such characters in a string literal, you use escape sequences. An escape sequence starts with a backslash "\" in the literal followed by one or more characters describing the desired character. For a newline character simply add "n" after the backslash.

Example: "Hello,\nworld!" If you were to replace the above "Hello, world!" literal with this one, the program would write <b>Hello, world!</b> on the console.

Other useful escape sequences are \" which adds a double quotation mark (without the backslash, the quotation mark would terminate the string literal) and \\ to add one backslash to the literal.

Example: "\"Interesting, \\\nor not?\", he said" Output: <b>"Interesting, \ or not?", he said</b>

Substrings
Sometimes you need to just grab part of a string. To do this use Array slice notation, which is the two '.'s between the indices you want to get the substring of lower bound inclusive and upper bound exclusive. Say we just wanted "Interesting" from the string seen above, we would do the following.

Example: "\"Interesting, \\\nor not?\", he said" [ 1 .. 12 ] Output: Interesting

(Stub)