Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Foreword by Andrei Alexandrescu
Preface
Acknowledgments
The Hello World Program
Compiler installation
Source file
Compiling the hello world program
Compiler switches
IDE
Contents of the hello world program
Exercises
writeln and write
Exercises
Compilation
Machine code
Programming language
Interpreter
Compiler
Fundamental Types
Properties of types
size_t
Exercise
Assignment and Order of Evaluation
The assignment operation
Order of evaluation
Exercise
Variables
Exercise
Standard Input and Output Streams
Exercise
Reading from the Standard Input
Skipping the whitespace characters
Additional information
Exercise
Logical Expressions
Logical Expressions
Grouping expressions
Reading bool input
Exercises
if Statement
The if block and its scope
The else block and its scope
Always use the scope curly brackets
The "if, else if, else" chain
Exercises
while Loop
The continue statement
The break statement
Infinite loop
Exercises
Integers and Arithmetic Operations
Exercises
Floating Point Types
Floating point type properties
.nan
Specifying floating point values
Overflow is not ignored
Precision
There is no truncation in division
Which type to use
Cannot represent all values
Comparing floating point values
Exercises
Arrays
Definition
Containers and elements
Accessing the elements
Index
Fixed-length arrays vs. dynamic arrays
Using .length to get or set the number of elements
An array example
Initializing the elements
Basic array operations
Exercises
Characters
History
Unicode encodings
The character types of D
Character literals
Control characters
Single quote and backslash
The std.uni module
Limited support for ı and i
Problems with reading characters
D's Unicode support
Summary
Slices and Other Array Features
Slices
Using $, instead of array.length
Using .dup to copy
Assignment
Making a slice longer may terminate sharing
Operations on all elements
Multi-dimensional arrays
Summary
Exercise
Strings
readln and strip, instead of readf
formattedRead for parsing strings
Double quotes, not single quotes
string, wstring, and dstring are immutable
Potentially confusing length of strings
String literals
String concatenation
Comparing strings
Lowercase and uppercase are different
Exercises
Redirecting the Standard Input and Output Streams
Redirecting the standard output to a file with operator >
Redirecting the standard input from a file with operator <
Redirecting both standard streams
Piping programs with operator |
Exercise
Files
Fundamental concepts
std.stdio.File struct
Exercise
auto and typeof
auto
typeof
Exercise
Name Scope
Defining names closest to their first use
for Loop
The sections of the while loop
The sections of the for loop
The sections may be empty
The name scope of the loop variable
Exercises
Ternary Operator ?:
The type of the ternary expression
Exercise
Literals
Integer literals
Floating point literals
Character literals
String literals
Literals are calculated at compile time
Exercises
Formatted Output
format_character
width
precision
flags
Positional parameters
Formatted element output
format
Exercises
Formatted Input
Format specifier characters
Exercise
do-while Loop
Exercise
Associative Arrays
Definition
Adding key-value pairs
Initialization
Removing key-value pairs
Determining the presence of a key
Properties
Example
Exercises
foreach Loop
The foreach syntax
continue and break
foreach with arrays
foreach with strings and std.range.stride
foreach with associative arrays
foreach with number ranges
foreach with structs, classes, and ranges
The counter is automatic only for arrays
The copy of the element, not the element itself
The integrity of the container must be preserved
foreach_reverse to iterate in the reverse direction
Exercise
switch and case
The goto statement
The expression must be an integer, string, or bool type
Value ranges
Distinct values
The final switch statement
When to use
Exercises
enum
Effects of magic constants on code quality
The enum syntax
Actual values and base types
enum values that are not of an enum type
Properties
Converting from the base type
Exercise
Functions
Parameters
Calling a function
Doing work
The return value
The return statement
void functions
The name of the function
Code quality through functions
Exercises
Immutability
Immutable variables
Immutable parameters
Immutability of the slice versus the elements
How to use
Summary
Value Types and Reference Types
Value types
Reference variables
Reference types
Fixed-length arrays are value types, slices are reference types
Experiment
Summary
Function Parameters
Parameters are always copied
Referenced variables are not copied
Parameter qualifiers
Summary
Exercise
Lvalues and Rvalues
Limitations of rvalues
Using auto ref parameters to accept both lvalues and rvalues
Terminology
Lazy Operators
Program Environment
The return value of main()
Standard error stream stderr
Parameters of main()
Command line options and the std.getopt module
Environment variables
Starting other programs
Summary
Exercises
Exceptions
The throw statement to throw exceptions
The try-catch statement to catch exceptions
Exception properties
Kinds of errors
Summary
scope
assert and enforce
Syntax
static assert
assert even if absolutely true
No value nor side effect
Disabling assert checks
enforce for throwing exceptions
How to use
Exercises
Unit Testing
Causes of bugs
Discovering the bugs
Unit testing for catching bugs
Activating the unit tests
unittest blocks
Testing for exceptions
Test driven development
Exercise
Contract Programming
in blocks for preconditions
out blocks for postconditions
Disabling contract programming
in blocks versus enforce checks
Exercise
Lifetimes and Fundamental Operations
Lifetime of a variable
Lifetime of a parameter
Fundamental operations
The null Value and the is Operator
The null value
The is operator
The !is operator
Assigning the null value
Summary
Type Conversions
Automatic type conversions
Explicit type conversions
Summary
Structs
Definition
Accessing the members
Construction
Copying and assignment
Struct literals
static members
Exercises
Variable Number of Parameters
Default arguments
Variadic functions
Exercise
Function Overloading
Overload resolution
Function overloading for user-defined types
Limitations
Exercise
Member Functions
Defining member functions
Exercises
const ref Parameters and const Member Functions
immutable objects
ref parameters that are not const
const ref parameters
Non-const member functions
const member functions
inout member functions
How to use
Constructor and Other Special Functions
Constructor
Destructor
Postblit
Assignment operator
Summary
Operator Overloading
Overloadable operators
Element indexing and slicing operators
Defining more than one operator at the same time
Return types of operators
opEquals() for equality comparisons
opCmp() for sorting
opCall() to call objects as functions
Indexing operators
Slicing operators
opCast for type conversions
Catch-all operator opDispatch
Inclusion query by opBinaryRight!"in"
Exercise
Classes
Comparing with structs
Summary
Inheritance
Warning: Inherit only if "is a"
Inheritance from at most one class
Hierarchy charts
Accessing superclass members
Constructing superclass members
Overriding the definitions of member functions
Using the subclass in place of the superclass
Inheritance is transitive
Abstract member functions and abstract classes
Example
Summary
Exercises
Object
typeid and TypeInfo
toString
opEquals
opCmp
toHash
Exercises
Interfaces
Definition
Inheriting from an interface
Inheriting from more than one interface
Inheriting from interface and class
Inheriting interface from interface
static member functions
final member functions
How to use
Abstraction
Example
Summary
destroy and scoped
An example of calling destructors late
destroy() to execute the destructor
When to use
Example
scoped() to call the destructor automatically
Summary
Modules and Libraries
Packages
Importing modules
Libraries
Encapsulation and Protection Attributes
Encapsulation
Protection attributes
Definition
Module imports are private by default
When to use encapsulation
Example
Universal Function Call Syntax (UFCS)
Properties
Calling functions without parentheses
Property functions that return values
Property functions that are used in assignment
Properties are not absolutely necessary
When to use
Contract Programming for Structs and Classes
Preconditions and postconditions for member functions
Preconditions and postconditions for object consistency
invariant() blocks for object consistency
Contract inheritance
Summary
Templates
Function templates
More than one template parameter
Type deduction
Explicit type specification
Template instantiation
Template specializations
Struct and class templates
Default template parameters
Every different template instantiation is a distinct type
A compile-time feature
Class template example: stack data structure
Function template example: binary search algorithm
Summary
Pragmas
pragma(msg)
pragma(lib)
pragma(inline)
pragma(startaddress)
pragma(mangle)
alias and with
alias
with
Summary
alias this
Multiple inheritance
Pointers
The concept of a reference
Syntax
Pointer value and the address-of operator &
The access operator *
The . (dot) operator to access a member of the pointee
Modifying the value of a pointer
Pointers are risky
The element one past the end of an array
Using pointers with the array indexing operator []
Producing a slice from a pointer
void* can point at any type
Using pointers in logical expressions
new returns a pointer for some types
The .ptr property of arrays
The in operator of associative arrays
When to use pointers
Examples
Exercises
Bit Operations
Representation of data at the lowest level
Binary number system
Hexadecimal number system
Bit operations
Semantics
Common uses
Exercises
Conditional Compilation
debug
version(tag) and version(level)
Assigning identifiers to debug and version
static if
static assert
Type traits
Summary
is Expression
is (T)
is (T Alias)
is (T : OtherT)
is (T Alias : OtherT)
is (T == Specifier)
is (T identifier == Specifier)
is (/* ... */ Specifier, TemplateParamList)
Function Pointers, Delegates, and Lambdas
Function pointers
Anonymous functions
Delegates
toString() with a delegate parameter
Summary
foreach with Structs and Classes
foreach support by range member functions
foreach support by opApply and opApplyReverse member functions
Loop counter
Warning: The collection must not mutate during the iteration
Exercises
Nested Functions, Structs, and Classes
Summary
Unions
Anonymous unions
Dissecting other members
Examples
Labels and goto
goto
Loop labels
goto in case sections
Summary
Tuples
Tuple and tuple()
AliasSeq
.tupleof property
Summary
More Templates
The shortcut syntax
Kinds of templates
Kinds of template parameters
typeof(this), typeof(super), and typeof(return)
Template specializations
Meta programming
Compile-time polymorphism
Code bloat
Template constraints
Using templates in multi-dimensional operator overloading
Summary
More Functions
Return type attributes
Behavioral attributes
Code safety attributes
Compile time function execution (CTFE)
Summary
Mixins
Template mixins
String mixins
Mixin name spaces
String mixins in operator overloading
Example
Ranges
History
Ranges are an integral part of D
Traditional implementations of algorithms
Phobos ranges
InputRange
ForwardRange
BidirectionalRange
RandomAccessRange
OutputRange
Range templates
Summary
More Ranges
Range kind templates
ElementType and ElementEncodingType
More range templates
Run-time polymorphism with inputRangeObject() and outputRangeObject()
Summary
Parallelism
taskPool.parallel()
Task
taskPool.asyncBuf()
taskPool.map()
taskPool.amap()
taskPool.reduce()
Multiple functions and tuple results
TaskPool
Summary
Message Passing Concurrency
Concepts
Starting threads
Thread identifiers
Message Passing
Expecting different types of messages
Waiting for messages up to a certain time
Exceptions during the execution of the worker
Detecting thread termination
Mailbox management
Priority messages
Thread names
Summary
Data Sharing Concurrency
Sharing is not automatic
shared to share mutable data between threads
A race condition example
synchronized to avoid race conditions
shared static this() for single initialization and shared static ~this() for single finalization
Atomic operations
Summary
Fibers
Call stack
Usage
Fibers in range implementations
Fibers in asynchronous input and output
Exceptions and fibers
Cooperative multitasking
Summary
Memory Management
Memory
The garbage collector
Allocating memory
Alignment
Constructing variables at specific memory locations
Destroying objects explicitly
Constructing objects at run time by name
Summary
User Defined Attributes (UDA)
Example
The benefit of user defined attributes
Summary
Operator Precedence
Exercise Solutions
The Hello World Program
writeln and write
Fundamental Types
Assignment and Order of Evaluation
Variables
Standard Input and Output Streams
Reading from the Standard Input
Logical Expressions
The if Statement
The while Loop
Integers and Arithmetic Operations
Floating Point Types
Arrays
Slices and Other Array Features
Strings
Redirecting the Standard Input and Output Streams
Files
auto and typeof
The for Loop
The Ternary Operator ?:
Literals
Formatted Output
Formatted Input
The do-while Loop
Associative Arrays
The foreach Loop
switch and case
enum
Functions
Function Parameters
Program Environment
assert and enforce
Unit Testing
Contract Programming
Structs
Variable Number of Parameters
Function Overloading
Member Functions
Operator Overloading
Inheritance
Object
Pointers
Bit Operations
foreach with Structs and Classes
Index
A
B
C
D
E
F
G
H
I
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
← Prev
Back
Next →
← Prev
Back
Next →