Contents....5
Foreword by Andrei Alexandrescu....17
Preface....19
Acknowledgments....19
The Hello World Program....21
Compiler installation....21
Source file....21
Compiling the hello world program....21
Compiler switches....22
IDE....23
Contents of the hello world program....23
Exercises....24
writeln and write....25
Exercises....25
Compilation....26
Machine code....26
Programming language....26
Interpreter....26
Compiler....27
Compilation error....27
Fundamental Types....28
Properties of types....29
size_t....30
Exercise....30
Assignment and Order of Evaluation....31
The assignment operation....31
Order of evaluation....31
Exercise....31
Variables....32
Exercise....33
Standard Input and Output Streams....34
Exercise....34
Reading from the Standard Input....35
Skipping the whitespace characters....36
Additional information....37
Exercise....37
Logical Expressions....38
Logical Expressions....38
Grouping expressions....41
Reading bool input....41
Exercises....41
if Statement....44
The if block and its scope....44
The else block and its scope....44
Always use the scope curly brackets....45
The "if, else if, else" chain....45
Exercises....47
while Loop....48
The continue statement....48
The break statement....49
Unconditional loop....49
Exercises....50
Integers and Arithmetic Operations....51
Integer types....52
Number of bits of a type....52
Choosing a type....53
Overflow....53
Truncation....53
.min and .max....54
Increment: ++....54
Decrement: --....54
Addition: +....55
Subtraction: -....55
Multiplication: *....56
Division: /....56
Remainder (modulus): %....56
Power: ^^....57
Arithmetic operations with assignment....57
Negation: -....57
Plus sign: +....58
Post-increment: ++....58
Post-decrement: --....58
Operator precedence....59
Detecting overflow....59
Preventing overflow....59
Preventing truncation....60
Exercises....61
Floating Point Types....62
Floating point type properties....62
.nan....63
Specifying floating point values....63
Observations....64
Overflow is not ignored....64
Precision....65
There is no truncation in division....65
Which type to use....66
Cannot represent all values....66
Unorderedness....67
isNaN() for .nan equality comparison....67
Exercises....68
Arrays....69
Definition....69
Containers and elements....70
Accessing the elements....70
Index....71
Fixed-length arrays vs. dynamic arrays....71
Using .length to get or set the number of elements....71
An array example....72
Initializing the elements....72
Basic array operations....73
Copying fixed-length arrays....73
Adding elements to dynamic arrays....73
Removing elements from dynamic arrays....74
Combining arrays....74
Sorting the elements....75
Reversing the elements....75
Exercises....75
Characters....77
History....77
ASCII Table....77
IBM Code Pages....77
ISO/IEC 8859 Code Pages....78
Unicode....78
Unicode encodings....78
The character types of D....79
Character literals....79
Control characters....80
Single quote and backslash....80
The std.uni module....81
Limited support for ı and i....82
Problems with reading characters....82
D's Unicode support....83
Summary....84
Slices and Other Array Features....85
Slices....85
Using $, instead of array.length....86
Using .dup to copy....86
Assignment....87
Making a slice longer may terminate sharing....87
Using capacity to determine whether sharing will be terminated....89
Reserving room for elements....90
Operations on all elements....90
Multi-dimensional arrays....92
Summary....94
Exercise....94
Strings....95
readln and strip, instead of readf....95
formattedRead for parsing strings....96
Double quotes, not single quotes....97
string, wstring, and dstring are immutable....97
Potentially confusing length of strings....98
String literals....99
String concatenation....99
Comparing strings....100
Lowercase and uppercase are different....100
Exercises....101
Redirecting the Standard Input and Output Streams....102
Redirecting the standard output to a file with operator >....102
Redirecting the standard input from a file with operator <....102
Redirecting both standard streams....103
Piping programs with operator |....103
Exercise....103
Files....104
Fundamental concepts....104
The producer and the consumer....104
Access rights....104
Opening a file....104
Closing a file....105
Writing to and reading from files....105
eof() to determine the end of a file....105
The std.file module....105
std.stdio.File struct....106
Writing to a file....106
Reading from a file....106
Exercise....107
auto and typeof....108
auto....108
typeof....108
Exercise....109
Name Scope....110
Defining names closest to their first use....110
for Loop....112
The sections of the while loop....112
The sections of the for loop....112
The sections may be empty....113
The name scope of the loop variable....114
Exercises....114
Ternary Operator ?:....115
The type of the ternary expression....116
Exercise....117
Literals....118
Integer literals....118
The types of integer literals....118
The L suffix....119
The U suffix....119
Floating point literals....120
The types of floating point literals....120
Character literals....120
String literals....121
Double-quoted string literals....121
Wysiwyg string literals....121
Delimited string literals....121
Token string literals....122
Types of string literals....122
Literals are calculated at compile time....122
Exercises....122
Formatted Output....123
format_character....124
width....126
separator....126
precision....127
flags....127
Positional parameters....128
Formatted element output....129
format....130
Checked format string....131
Exercises....131
Formatted Input....132
Format specifier characters....133
Exercise....133
do-while Loop....134
Exercise....135
Associative Arrays....136
Definition....136
Adding key-value pairs....137
Initialization....137
Removing key-value pairs....137
Determining the presence of a key....138
Properties....138
Example....139
Exercises....139
foreach Loop....140
The foreach syntax....140
continue and break....141
foreach with arrays....141
foreach with strings and std.range.stride....141
foreach with associative arrays....142
foreach with number ranges....142
foreach with structs, classes, and ranges....142
The counter is automatic only for arrays....143
The copy of the element, not the element itself....143
The integrity of the container must be preserved....144
foreach_reverse to iterate in the reverse direction....144
Exercise....145
switch and case....146
The goto statement....146
The expression must be an integer, string, or bool type....148
Value ranges....149
Distinct values....149
The final switch statement....149
When to use....150
Exercises....150
enum....151
Effects of magic constants on code quality....151
The enum syntax....151
Actual values and base types....152
enum values that are not of an enum type....152
Properties....153
Converting from the base type....154
Exercise....154
Functions....155
Parameters....156
Calling a function....158
Doing work....158
The return value....158
The return statement....159
void functions....159
The name of the function....160
Code quality through functions....160
Code duplication is harmful....160
Commented lines of code as functions....164
Exercises....164
Immutability....166
Immutable variables....166
enum constants....166
immutable variables....168
const variables....169
Immutable parameters....169
const parameters....170
immutable parameters....171
Should a parameter be const or immutable?....171
Immutability of the slice versus the elements....173
const and immutable are transitive....174
.dup and .idup....174
How to use....174
Summary....175
Value Types and Reference Types....176
Value types....176
The use of assert checks below....176
Value identity....177
Address-of operator, &....177
Reference variables....177
Reference types....178
The difference in the assignment operation....180
Variables of reference types may not be referencing any object....181
Fixed-length arrays are value types, slices are reference types....182
Experiment....182
Summary....184
Function Parameters....185
Parameters are always copied....185
Referenced variables are not copied....186
Surprising reference semantics of slices....186
Surprising reference semantics of associative arrays....187
Parameter qualifiers....188
in....188
out....188
const....189
immutable....190
ref....190
auto ref....191
inout....191
lazy....193
scope....194
shared....194
return....195
Summary....196
Exercise....197
Lvalues and Rvalues....198
Limitations of rvalues....198
Rvalues don't have memory addresses....198
Rvalues cannot be assigned new values....198
Rvalues cannot be passed to functions by reference....199
Using auto ref parameters to accept both lvalues and rvalues....199
Terminology....200
Lazy Operators....201
Program Environment....202
The return value of main()....202
main() always returns a value....202
Specifying the return value....203
Standard error stream stderr....204
Parameters of main()....204
Command line options and the std.getopt module....205
Environment variables....207
Starting other programs....207
Summary....208
Exercises....208
Exceptions....209
The throw statement to throw exceptions....209
The exception types Exception and Error....210
Thrown exception terminates all scopes....210
When to use throw....214
The try-catch statement to catch exceptions....214
catch blocks are considered sequentially....216
The finally block....218
When to use the try-catch statement....218
Exception properties....219
Kinds of errors....220
User errors....220
Programmer errors....221
Unexpected situations....222
Summary....222
scope....223
assert and enforce....225
Syntax....225
static assert....226
assert even if absolutely true....227
No value nor side effect....227
Disabling assert checks....228
enforce for throwing exceptions....228
How to use....228
Exercises....229
Unit Testing....231
Causes of bugs....231
Discovering the bugs....231
Unit testing for catching bugs....232
Activating the unit tests....233
unittest blocks....233
Testing for exceptions....234
Test driven development....235
Unit tests before bug fixes....236
Exercise....237
Contract Programming....238
in blocks for preconditions....238
out blocks for postconditions....239
Disabling contract programming....241
in blocks versus enforce checks....241
Exercise....242
Lifetimes and Fundamental Operations....244
Lifetime of a variable....244
Lifetime of a parameter....244
Fundamental operations....245
Initialization....246
Finalization....247
Assignment....248
The null Value and the is Operator....249
The null value....249
The is operator....250
The !is operator....250
Assigning the null value....250
Summary....251
Type Conversions....253
Automatic type conversions....253
Integer promotions....254
Arithmetic conversions....254
Slice conversions....255
const conversions....256
immutable conversions....256
enum conversions....257
bool conversions....257
Explicit type conversions....258
Construction syntax....258
to() for most conversions....259
assumeUnique() for fast immutable conversions....259
The cast operator....260
Summary....261
Structs....262
Definition....262
struct defines a type, not a variable....263
Coding convenience....263
Accessing the members....264
Construction....265
Constructing objects as immutable....266
Trailing members need not be specified....266
Specifying default values for members....267
Constructing by the {} syntax....267
Copying and assignment....267
Careful with members that are of reference types!....268
Struct literals....269
static members....269
static this() for initialization and static ~this() for finalization....271
Exercises....272
Variable Number of Parameters....274
Default arguments....274
Special keywords as default arguments....275
Variadic functions....276
Variadic function arguments have a short lifetime....278
Exercise....279
Function Overloading....281
Overload resolution....282
Function overloading for user-defined types....282
Limitations....283
Exercise....284
Member Functions....285
Defining member functions....285
toString() for string representations....286
Example: increment() member function....287
Exercises....289
const ref Parameters and const Member Functions....291
immutable objects....291
ref parameters that are not const....291
const ref parameters....291
Non-const member functions....292
const member functions....292
inout member functions....293
How to use....294
Constructor and Other Special Functions....295
Constructor....295
Constructor syntax....295
Compiler-generated automatic constructor....296
Accessing the members by this.....296
User-defined constructors....297
First assignment to a member is construction....298
User-defined constructor disables compiler-generated constructor....298
static opCall instead of the default constructor....299
Calling other constructors....300
Constructor qualifiers....300
Immutability of constructor parameters....302
Type conversions through single-parameter constructors....303
Disabling the default constructor....303
Destructor....304
Destructor is executed automatically....304
Destructor example....305
Postblit....307
Disabling postblit....308
Assignment operator....308
Assigning from other types....309
Summary....310
Operator Overloading....311
Overloadable operators....313
Unary operators....313
Binary operators....314
Element indexing and slicing operators....315
Other operators....316
Defining more than one operator at the same time....316
Return types of operators....317
opEquals() for equality comparisons....319
opCmp() for sorting....320
opCall() to call objects as functions....322
Indexing operators....323
Indexing operators example....324
Slicing operators....326
opCast for type conversions....328
Catch-all operator opDispatch....330
Inclusion query by opBinaryRight!"in"....330
Example of the in operator....331
Exercise....332
Classes....335
Comparing with structs....335
Classes are reference types....335
Class variables may be null....335
Class variables versus class objects....336
Copying....336
Assignment....337
Definition....337
Construction....338
Destruction....338
Member access....339
Operator overloading....339
Member functions....339
The is and !is operators....340
Summary....340
Inheritance....341
Warning: Inherit only if "is a"....343
Inheritance from at most one class....343
Hierarchy charts....344
Accessing superclass members....344
Constructing superclass members....345
Overriding the definitions of member functions....346
Using the subclass in place of the superclass....347
Inheritance is transitive....348
Abstract member functions and abstract classes....349
Example....350
Summary....353
Exercises....353
Object....355
typeid and TypeInfo....355
toString....357
opEquals....357
opCmp....360
opCmp for string members....361
toHash....362
Hash table indexes....362
Selecting members for toHash....363
Calculating hash values....363
Hash values for structs....364
Exercises....364
Interfaces....367
Definition....367
Inheriting from an interface....367
Inheriting from more than one interface....368
Inheriting from interface and class....369
Inheriting interface from interface....369
static member functions....369
final member functions....371
How to use....372
Abstraction....372
Example....373
Summary....374
destroy and scoped....376
An example of calling destructors late....376
destroy() to execute the destructor....377
When to use....377
Example....377
scoped() to call the destructor automatically....380
Summary....381
Modules and Libraries....383
static this() and static ~this()....383
File and module names....383
Packages....384
Importing modules....384
Selective imports....385
Local imports....385
Locations of modules....386
Long and short module names....386
Renamed imports....386
Importing a package as a module....387
Deprecating features....387
Adding module definitions to the program....388
Libraries....389
Using libraries of other languages....389
Encapsulation and Protection Attributes....392
Encapsulation....393
Protection attributes....393
Definition....394
Module imports are private by default....394
When to use encapsulation....395
Example....396
Universal Function Call Syntax (UFCS)....398
Properties....401
Calling functions without parentheses....401
Property functions that return values....401
Property functions that are used in assignment....402
Properties are not absolutely necessary....403
When to use....404
Contract Programming for Structs and Classes....406
Preconditions and postconditions for member functions....406
Preconditions and postconditions for object consistency....407
invariant() blocks for object consistency....408
Contract inheritance....409
Unintentionally disabling preconditions....411
Summary....411
Templates....413
Function templates....414
More than one template parameter....415
Type deduction....416
Explicit type specification....416
Template instantiation....417
Template specializations....417
Struct and class templates....418
Default template parameters....420
Every template instantiation yields a distinct type....421
A compile-time feature....421
Class template example: stack data structure....421
Function template example: binary search algorithm....424
Summary....426
Pragmas....428
pragma(msg)....428
pragma(lib)....428
pragma(inline)....428
pragma(startaddress)....430
pragma(mangle)....430
alias and with....432
alias....432
Shortening a long name....432
Design flexibility....433
Revealing hidden names of superclasses....434
with....436
Summary....437
alias this....438
Multiple inheritance....438
Pointers....441
The concept of a reference....441
The ref variables in foreach loops....441
ref function parameters....442
Reference types....442
Syntax....443
Pointer value and the address-of operator &....444
The access operator *....445
The . (dot) operator to access a member of the pointee....445
Modifying the value of a pointer....446
Pointers are risky....448
The element one past the end of an array....448
Using pointers with the array indexing operator []....449
Producing a slice from a pointer....450
void* can point at any type....450
Using pointers in logical expressions....451
new returns a pointer for some types....452
The .ptr property of arrays....453
The in operator of associative arrays....453
When to use pointers....454
When required by libraries....454
When referencing variables of value types....454
As member variables of data structures....455
When accessing memory directly....455
Examples....455
A simple linked list....455
Observing the contents of memory by ubyte*....457
Exercises....460
Bit Operations....462
Representation of data at the lowest level....462
Transistor....462
Bit....462
Byte....462
Register....462
Binary number system....463
The sign bit of signed integer types....464
Hexadecimal number system....464
Bit operations....466
Complement operator: ~....466
And operator: &....467
Or operator: |....467
Xor operator: ^....468
Right-shift operator: >>....468
Unsigned right-shift operator: >>>....469
Left-shift operator: <<....469
Operators with assignment....470
Semantics....470
| is a union set....470
& is an intersection set....470
|= sets selected bits to 1....471
&= clears selected bits....471
& determines whether a bit is 1 or not....471
Right-shifting by one is the equivalent of dividing by two....472
Left-shifting by one is the equivalent of multiplying by two....472
Common uses....472
Flags....472
Masking....473
Exercises....474
Conditional Compilation....476
debug....476
debug(tag)....478
debug(level)....478
version(tag) and version(level)....479
Assigning identifiers to debug and version....480
static if....480
static assert....481
Type traits....482
Summary....483
is Expression....484
is (T)....484
is (T Alias)....484
is (T : OtherT)....485
is (T Alias : OtherT)....485
is (T == Specifier)....485
Whether the same type....485
Whether matches the same specifier....486
is (T identifier == Specifier)....487
is (/* ... */ Specifier, TemplateParamList)....488
Function Pointers, Delegates, and Lambdas....491
Function pointers....491
Member function pointers....491
Definition....492
Calling a function pointer....492
When to use....492
Function pointer as a parameter....493
Function pointer as a member....495
Anonymous functions....496
Shorter syntax....496
Lambda syntax instead of a single return statement....497
Delegates....499
Shorter syntax....500
An object and a member function as a delegate....500
Delegate properties....502
Lazy parameters are delegates....502
Lazy variadic functions....503
toString() with a delegate parameter....503
Summary....505
foreach with Structs and Classes....507
foreach support by range member functions....507
Example....508
std.range.retro to iterate in reverse....508
foreach support by opApply and opApplyReverse member functions....509
Overloading opApply to iterate in different ways....511
Loop counter....512
Loop counter with range functions....512
Loop counter with opApply....513
Warning: The collection must not mutate during the iteration....514
Exercises....515
Nested Functions, Structs, and Classes....516
static when a closure is not needed....517
Classes nested inside classes....518
Summary....519
Unions....520
Anonymous unions....521
Dissecting other members....521
Examples....522
Communication protocol....522
Discriminated union....523
Labels and goto....526
goto....526
Finalization area....526
continue and break for outer loops....527
The problem of skipping constructors....527
Loop labels....528
goto in case sections....528
Summary....528
Tuples....529
Tuple and tuple()....529
Member properties....529
Expanding the members as a list of values....530
Compile-time foreach....530
Returning multiple values from functions....531
AliasSeq....532
AliasSeq consisting of values....532
Indexing and slicing....533
AliasSeq consisting of types....533
foreach with AliasSeq....534
.tupleof property....534
Summary....535
More Templates....536
The shortcut syntax....536
Template name space....537
Eponymous templates....537
Kinds of templates....538
Function, class, and struct templates....538
Member function templates....538
Union templates....539
Interface templates....541
Kinds of template parameters....541
Type template parameters....541
Value template parameters....541
this template parameters for member functions....544
alias template parameters....545
Tuple template parameters....547
typeof(this), typeof(super), and typeof(return)....549
Template specializations....550
Meta programming....550
Compile-time polymorphism....552
Code bloat....553
Template constraints....554
Tuple parameter of single element....555
Named constraints....555
Using templates in multi-dimensional operator overloading....557
Multi-dimensional operator overloading example....559
Summary....562
More Functions....563
Return type attributes....563
auto functions....563
ref functions....563
auto ref functions....565
inout functions....565
Behavioral attributes....566
pure functions....566
nothrow functions....570
@nogc functions....571
Code safety attributes....571
@safe functions....572
@trusted functions....572
@system functions....572
Compile time function execution (CTFE)....572
The __ctfe variable....574
Summary....574
Mixins....576
Template mixins....576
Template mixins must use local imports....577
Identifying the type that is mixing in....578
String mixins....578
Mixin name spaces....579
String mixins in operator overloading....580
Mixed in destructors....581
Importing text files....581
Example....582
Ranges....584
History....584
Ranges are an integral part of D....585
Traditional implementations of algorithms....585
Phobos ranges....586
Iterating by shortening the range....587
InputRange....588
InputRange example....588
The std.array module to use slices as ranges....590
Automatic decoding of strings as ranges of dchar....591
Ranges without actual elements....592
Infinite ranges....593
Functions that return ranges....594
std.range and std.algorithm modules....595
Laziness....597
ForwardRange....597
BidirectionalRange....599
RandomAccessRange....600
Infinite RandomAccessRange....600
Finite RandomAccessRange....602
OutputRange....606
Using slices as OutputRange....607
Range templates....609
Summary....609
More Ranges....610
Range kind templates....610
ElementType and ElementEncodingType....613
More range templates....613
Run-time polymorphism with inputRangeObject() and outputRangeObject()....614
Summary....615
static foreach....616
Parallelism....619
taskPool.parallel()....621
Work unit size....622
Task....622
Exceptions....624
Member functions of Task....625
taskPool.asyncBuf()....626
taskPool.map()....628
taskPool.amap()....630
taskPool.reduce()....631
Multiple functions and tuple results....634
TaskPool....634
Summary....635
Message Passing Concurrency....636
Concepts....636
Starting threads....637
Thread identifiers....638
Message Passing....639
Example....640
Expecting different types of messages....643
Receiving any type of message....644
Waiting for messages up to a certain time....645
Exceptions during the execution of the worker....645
Detecting thread termination....648
OwnerTerminated exception....648
LinkTerminated exception....649
Receiving exceptions as messages....649
Mailbox management....650
Priority messages....651
Thread names....651
Summary....653
Data Sharing Concurrency....654
Sharing is not automatic....654
shared to share mutable data between threads....655
A race condition example....656
synchronized to avoid race conditions....657
shared static this() for single initialization and shared static ~this() for single finalization....661
Atomic operations....662
atomicOp....662
cas....663
Summary....664
Fibers....665
Call stack....665
Recursion....666
Usage....667
Fibers in range implementations....668
std.concurrency.Generator for presenting fibers as ranges....672
Fibers in asynchronous input and output....674
Exceptions and fibers....678
Cooperative multitasking....679
Summary....680
Memory Management....681
Memory....681
The garbage collector....681
Starting and delaying garbage collection cycles....682
Allocating memory....683
Memory block attributes....685
Example of extending a memory area....687
Alignment....688
The .alignof property....688
The .offsetof property....690
The align attribute....692
Constructing variables at specific memory locations....693
Constructing a struct object at a specific location....693
Constructing a class object at a specific location....694
Destroying objects explicitly....697
Constructing objects at run time by name....698
Summary....699
User Defined Attributes (UDA)....700
Example....702
The benefit of user defined attributes....704
Summary....705
Operator Precedence....706
Chaining....706
Associativity....707
Unordered operator groups....707
The precedence of =>....707
Comma operator....708
Exercise Solutions....709
The Hello World Program....709
writeln and write....709
Fundamental Types....709
Assignment and Order of Evaluation....709
Variables....710
Standard Input and Output Streams....710
Reading from the Standard Input....710
Logical Expressions....710
The if Statement....711
The while Loop....712
Integers and Arithmetic Operations....713
Floating Point Types....714
Arrays....715
Slices and Other Array Features....717
Strings....718
Redirecting the Standard Input and Output Streams....718
Files....718
auto and typeof....719
The for Loop....719
The Ternary Operator ?:....720
Literals....720
Formatted Output....721
Formatted Input....721
The do-while Loop....722
Associative Arrays....722
The foreach Loop....723
switch and case....723
enum....725
Functions....726
Function Parameters....726
Program Environment....727
assert and enforce....727
Unit Testing....729
Contract Programming....731
Structs....732
Variable Number of Parameters....734
Function Overloading....736
Member Functions....737
Operator Overloading....739
Inheritance....743
Object....746
Pointers....747
Bit Operations....750
foreach with Structs and Classes....750
Index....754
A....755
B....755
C....756
D....757
E....757
F....758
G....758
H....759
I....759
J....760
K....760
L....760
M....760
N....761
O....761
P....762
Q....763
R....763
S....763
T....764
U....765
V....766
W....766
X....766
Y....766
The main aim of this book is to teach D to readers who are new to computer programming. Although having experience in other programming languages is certainly helpful, this book starts from the basics.
D is a multi-paradigm system programming language that combines a wide range of powerful programming concepts from the lowest to the highest levels. It has C-like syntax and static typing. It pragmatically combines efficiency, control, and modeling power, with safety and programmer productivity in mind.
Each chapter is based on the contents of the previous ones, introducing as few new concepts as possible. It is recommended that the book is read in linear fashion, without skipping chapters if possible.
Although this book was written with beginners in mind, it covers almost all features of D. More experienced programmers can use the book as a D language reference by starting from the index section.