Cover ....1
Copyright....3
Credits....4
Foreword....6
About the Author....7
About the Reviewers....8
www.PacktPub.com....10
Table of Contents....12
Preface....22
Chapter 1: How to Get a D in Programming....28
Say hello to D....29
An introductory program....30
Getting help....34
The Digital Mars D compiler....36
Frontends, backends, and linkers....36
Installing DMD....38
The Windows installer....38
Installing from the ZIP....39
Compiling the example....40
Documentation and source code....41
The documentation....41
The source....43
Say hello to MovieMan....43
The problem....44
The features....44
DUB – the D build tool and package manager....45
Getting started....46
Configuring the MovieMan project....47
Understanding dub.sdl....48
Building and running MovieMan....49
Changing the output directory....51
Summary....51
Chapter 2: Building a Foundation with D Fundamentals....52
The very basics....53
Identifiers....53
A note about scope....53
More on modules....55
Module declarations....55
More about import declarations....57
The special package module....59
Comments....60
Variable declaration and initialization....61
Basic types....62
The types....63
Literals....64
Integer literals....64
Floating-point literals....65
Character literals....66
Conversions....66
Alias declarations....68
Properties....68
Basic operators....70
Arithmetic operators....70
Bitwise operators....71
Relational and logical operators....72
The cast operator ....73
Derived data types....73
Pointers....73
Arrays....75
Array basics....75
Rectangular arrays....78
Slices....80
Array literals....83
Arrays and void....84
Array operations....84
Strings....87
String essentials....87
Alternative string literals....89
Associative arrays....91
Control flow statements....92
Traditional loops....93
The foreach loop....94
Traditional conditionals....95
The goto statement....98
Type qualifiers – const and immutable....99
The contracts....99
With the basic types....99
With pointers....100
With arrays....102
Conversions....103
Functions....103
Overloaded functions....105
ref and out....105
inout parameters....108
lazy parameters....109
Function attributes....110
Return statements and memory....110
Function pointers and delegates....112
MovieMan – first steps....114
The io module....114
The app module....118
Summary....119
Chapter 3: Programming Objects the D Way....120
User-defined types....120
Enumerations....120
Unions....122
Structs and classes....122
Working with objects....125
Encapsulation with protection attributes....125
Public....125
Private....126
Package....127
Voldemort types....128
Constructors and destructors....129
Class constructors and destructors....129
Struct constructors and destructors....132
Static constructors and destructors....135
Inheritance and polymorphism....137
Interfaces....140
Fake inheritance....141
Nested classes....142
Objects with const and immutable....143
const as a storage class....145
Error handling....145
Scope guards....145
Exception handling....146
Contract programming and unit tests....149
Assert contracts....149
Function contracts....150
Invariants....150
Unit tests....152
MovieMan – adding menus....153
The Menu base class....153
The MainMenu class....155
The DisplayMenu class....157
Summary....157
Chapter 4: Running Code at Compile Time....158
Pragmas....159
The lib pragma....159
The msg pragma....160
The inline pragma....161
Conditional compilation....161
The version condition....161
The debug condition....164
The static if condition....165
Compile-time strings....166
The import expression....166
String mixins....167
Compile-time function execution....168
Odds and ends....172
static assert....172
The is expression....173
Alignment....174
Compile-time reflection....180
User-defined attributes....181
Summary....184
Chapter 5: Generic Programming Made Easy....186
Template basics....187
Templates as code blocks....187
Struct and class templates....190
Enum templates....193
Function templates....193
Special features....195
More template parameters....196
Value parameters....196
Alias parameters....197
This parameters....199
Beyond the basics....200
Template specializations....200
Specialization on pointers and arrays....202
Template constraints....204
Template mixins....205
Variadic templates....207
Terminology....209
More on usage....211
Operator overloading....213
Non-templated operator overloads....213
Comparison overloads – opEquals and opCmp....213
Function call overloads....216
Assignment overloads....217
Index overloads....218
Templated operator overloads....219
Unary overloads....219
Binary overloads....220
Cast overloads....222
Operator assignment overloads....222
Slice operator overloads....223
Other overloads....225
opDispatch....225
opApply....227
toHash....228
MovieMan – the database....229
db.d....229
Back to the menus....231
Summary....233
Chapter 6: Understanding Ranges....234
Ranges defined....234
The problem....235
The solution....237
The interface....237
A range for arrays....239
The implementation of filter....240
The test....242
The real ranges....243
Input ranges....243
Forward ranges....245
Bidirectional ranges....246
Random-access ranges....246
Output ranges....247
Optional range primitives....248
Ranges in use....250
Custom ranges....251
Getting a range from a stack....251
A name generator range....253
Other considerations....255
Custom algorithms....256
Summary....257
Chapter 7: Composing Functional Pipelines with Algorithms and Ranges....258
Functional programming and composable pipelines....259
A simple example....260
A more complex example....263
Sometimes we can't....265
Navigating Phobos....266
std.range....266
Generative ranges....267
Selective ranges....269
Compositional ranges....271
std.algorithm....274
Comparison....274
Iteration....276
Mutation....278
Searching....280
Set operations....281
Sorting....282
std.array....284
Appender....284
assocArray....285
join....285
Where to look for more....286
MovieMan – wrapping up....286
The db module....286
The display menu....290
Making it better....295
Summary....295
Chapter 8: Exploring the Wide World of D....296
Online resources....297
DWiki....297
Planet D....297
reddit and StackOverflow....297
This Week in D....298
DConf....299
DSource....300
Editors and IDEs....301
Text editors....301
Vim and Emacs....301
Textadept....301
Sublime Text....302
IDEs....303
Visual D....303
Mono-D....303
DDT....303
Code::Blocks....304
Tools and utilities....304
DMD....304
Optimized and debug builds....304
Changing the default output....305
Compiling libraries....305
Using libraries....306
Warnings....308
Profiling....308
Code coverage analysis....310
Compile and run....312
GDC and LDC....312
GDC....312
LDC....313
RDMD....313
DustMite....314
DCD....316
DVM....316
Libraries....317
code.dlang.org....317
Using libraries from the DUB registry....317
Registering libraries with the DUB registry....320
Browsing the DUB registry....323
Deimos and DerelictOrg....323
Summary....324
Chapter 9: Connecting D with C....326
Preliminaries....327
Terminology....327
Bindings, wrappers, and ports....327
Dynamic and static – context matters....329
Object file formats....332
Linkage attributes....334
Name mangling....334
Calling conventions....335
Putting it together....336
Binding D to C....338
Function prototypes....339
Manually loading shared libraries....341
Trying it out....343
C types to D types....346
Strings and characters....347
Special types....347
Enumerations....348
Structures....349
Pointers....352
Type aliases....353
Function pointers....354
Defined constants....356
Function parameters and return types....356
Symbols....358
Global variables....359
Macros....360
Conditional compilation....364
Calling C from D....366
D arrays and C arrays....366
Basic arrays....366
Arrays of arrays....369
Strings....371
Memory....373
C callbacks and exceptions....374
Calling D from C....376
Summary....378
Chapter 10: Taking D Online....380
The software....381
vibe.d....381
Package overview....381
The anatomy of a vibe.d web app....382
The database library....384
MovieManWeb....386
Getting started....387
The basics of diet templates....390
Tags and indentation....391
Including and extending templates....393
The MovieManWeb layout....395
Setting up the database....397
Fleshing out the index page....401
Mapping web interface functions to URLs....401
Rendering diet templates....402
Rewriting index.dt....403
Adding movies....404
Implementing the addMovie function....405
Implementing the postAdd function....407
Implementing add.dt....409
Modifying index.dt....410
Modifying app.d....412
Modifying layout.dt....412
Listing movies....413
Implementing the listMovies function....413
Modifying the index function....415
Modifying index.dt....416
Finding movies....418
Implementing the postFind function....418
Implementing find.dt....419
Modifying index.dt....420
Modifying app.d and layout.dt....421
Implementing the findMovie functions....421
Editing and deleting movies....424
Expanding on MovieManWeb....426
Summary....427
Chapter 11: Taking D to the Next Level....428
Concurrency....429
Threads and fibers....429
Threads....429
Fibers....430
Data sharing....431
__gshared....432
Shared....432
Synchronization and atomics....433
Automatic synchronization....433
Manual synchronization....435
Atomics....435
Message passing....435
Parallelism....436
More information....438
SafeD....438
Functional purity....440
The garbage collector....441
Connecting with C++....442
More on Phobos....443
std.container....443
std.datetime....444
std.digest....444
std.experimental....444
std.getopt....445
std.process....446
std.socket....446
Modules for Unicode and other encodings....446
System bindings....447
Game development with D....447
The future of D....448
Summary....449
Index....450
Leverage the modern convenience and modelling power of the D programming language to develop software with native efficiency
This book is intended for those with some background in a C-family language who want to learn how to apply their knowledge and experience to D. Perhaps you're a college student looking to use D for hobby projects, or a career programmer interested in expanding your skillset. This book will help you get up to speed with the language and avoid common pitfalls that arise when translating C-family experience to D.
D is a modern programming language that is both powerful and efficient. It combines multiple paradigms in a way that opens up a whole new world of software design. It is used to develop both desktop and web applications, with future targets including mobile, and is available on multiple platforms. It is familiar to anyone with some experience in one or more of the C-family languages. However, hidden in the similarities are several differences that can be surprising when trying to apply common idioms from other languages. When learning D on your own, this can make it more time-consuming to master. In order to make the most of the language and become an idiomatic D programmer, it's necessary to learn how to think in D.
This book familiarizes you with D from the ground up, with a heavy focus on helping you to avoid surprises so that you can take your D knowledge to the next level more quickly and painlessly.
Your journey begins with a taste of the language and the basics of compiling D programs with DMD, the reference D compiler developed by Digital Mars, and DUB, a community-developed build utility and package manager. You then set out on an exploration of major language features. This begins with the fundamentals of D, including built-in types, conditionals, loops and all of the basic building-blocks of a D program, followed by an examination of D's object-oriented programming support. You'll learn how these features differ from languages you may already be familiar with. Next up are D's compile-time features, such as Compile-Time Function Evaluation and conditional compilation, then generic programming with templates. After that, you'll learn the more advanced features of ranges and functional pipeline programming. To enhance your D experience, you are next taken on a tour of the D ecosystem and learn how to make D interact with C. Finally, you get a look at D web development using the vibe.d project and the book closes with some handy advice on where to go next.
A friendly guide to the D programming language and its ecosystem that walks programmers through all they need to know for a painless experience in learning D.