D (The Programming Language)/d2/Types, Continued
Lesson 4: Types, Continued
editIn this lesson, you will see all the other types. Many of these types will be familiar, but be aware that there are many differences between C and D in their built-in types.
Introductory Code
editAll-in-One
editimport std.stdio;
void main()
{
bool a; // default initialized to false
a = 1;
a = false;
uint aa = 53; //unsigned int
ulong bb = 10395632; //unsigned long
short cc = 100;
ushort dd = cc; //dd == 100
byte b = 0x5E;
writeln(b); // prints 94
b = 110;
ubyte c = 255; //maximum value for a ubyte
assert(c == ubyte.max);
short d = 50;
long e = d;
writeln(e); // 50
float f; // default initialized to float.nan
float g = 3.13;
double h = g * 2.5;
// largest hardware implemented floating-point
real i = 373737373737377373.0;
writeln(i); // 3.73737e+17
// these numbers are not real:
idouble j; //default = double.nan * 1.0i
ifloat k;
ireal l;
cfloat m = 5 + 32.325i; // a complex number
// unsigned 8-bit UTF-8
char n; // default initialized to 0xFF
n = 'm';
writeln(n); // m
// unsigned 16-bit UTF-16
wchar o; // default = 0xFFFF
// unsigned 32-bit UTF-32
dchar p; // default = 0x0000FFFF
}
Concepts
editAssertion
editUsing assert
allows you to check if a certain expression is true or not. ==
means is equal to (do not confuse this with =
, which is for assigning variables). The checking is done at runtime, and if whatever is inside the parentheses evaluates to false, then an Assertion Error will happen and the program will terminate.
void main()
{
bool a = true;
assert(a);
assert(2346924); // anything that's not 0 or false
// assert(false); Assertion failure
int i = 4628;
assert(i == 4628); // (i == 4628) evaluates to true
assert(i != 4528); // (i != 4528) evaluates to true
assert(i >= 0); // yes, i is greater or equal to 0
}
Properties and Default Initializers
editTypes have default initializers. That means, if they are declared but not assigned any value, they will be equal to something by default.
int i; //i = int.init
assert(int.init == 0); // i is definitely 0
float b = float.init; //b = float.nan
Properties can be queried from any type or object. When a property of a type or object is queried, a dot goes between the property name and the identifier.
type.property
The init
property of any type contains the value that objects of such a type are initialized to by default. For numeral types, you can find the min and max values with their respective properties: type.min
and type.max
. You can also find the size of a type in memory: type.sizeof
.
If you want to declare but don't want to initialize something, you can do this:
int i = void;
Then, i is uninitialized; its value is undefined garbage.
A Bit of D's Lexical Grammar
editD allows you to write numbers in a few useful bases and forms. So, if you wanted to add the hexadecimal number of A4 to one million three hundred thirteen thousand six hundred thirty-seven and the binary number of 1011010101, you can write this:
import std.stdio;
void main()
{
writeln(0xA4 + 1_113_637 + 0b10_11_010101);
// hex numbers are prefixed by 0x
// binary numbers by 0b
// the answer is 1114526
}
Note that underscores in the middle of numbers are completely ignored. They are 100% optional and are useful for formatting long numbers.
Tips
edit- You may look through this reference for more information about D's lexical grammar. Soon a lesson will be written that contains up-to-date information on the subject.