D Programming Language Tutorial
Learn D Programming Language step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- Introduction to the D Programming Language Meet D, a compiled systems language with C-like speed, garbage collection, templates and compile-time execution. See what it is for and how this course teaches it.
- Installing D and Your First Program Install a D compiler on Windows, macOS or Linux, write and compile hello world, run scripts with rdmd, and understand what each part of a first D program does.
- Compilers, Flags and the Build Process Learn how a D program goes from source to executable, which dmd, ldc2 and gdc flags matter, how to build a multi-file project with dub, and how to read compiler errors.
- Program Structure, Imports and Printing in D Understand how a D program is organized: modules, selective imports, comments, statements, the print family writeln and writefln, and reading input with readln.
Basics and Types
- Variables, Types and Initialization in D Learn how D declares variables, how default initialization works, how auto infers types, and what const, immutable and enum mean for values that never change.
- Integers and Floating-Point Numbers in D Understand D's signed and unsigned integers, silent wrap-around on overflow, integer division, floating-point precision, NaN and infinity, and how to compare numbers safely.
- Characters, Strings and Unicode in D Learn how D stores text as UTF-8, the difference between char, wchar and dchar, why string is immutable, how to concatenate and slice, and how to count characters correctly.
- Type Conversion and Casting in D Learn which conversions D performs automatically, how integer promotion works, when to use std.conv.to for safe conversions, and when a cast is needed and dangerous.
- Operators, Expressions and Enums in D A tour of D's operators: arithmetic, comparison, logical, bitwise, the power and concatenation operators, in, is, the ternary operator, precedence rules, and named enums.
Control Flow and Functions
- Conditionals in D: if, else, switch and final switch Make decisions in D with if and else, the ternary operator, switch with strings and case ranges, final switch over enums, and static if for compile-time conditions.
- Loops in D: for, while, do-while and foreach Repeat work in D with for, while, do-while and the powerful foreach over ranges, arrays and associative arrays, plus break, continue, labels and foreach_reverse.
- Functions in D: Parameters, Return Values and Attributes Define and call D functions, pass arguments by value or by ref, use default and in/out parameters, return multiple values with tuples, and apply attributes like pure and nothrow.
- UFCS, Lambdas, Delegates and Variadic Functions in D Explore D's function features: uniform function call syntax, overloading, function pointers, lambdas, delegates that capture variables, and variadic functions.
- Errors, Exceptions, Scope Guards and Contracts in D Handle errors in D with try, catch and finally, clean up with scope(exit) and scope(failure), state preconditions with in and out contracts, and test code with unittest blocks.
Arrays and Ranges
- Arrays in D: Static and Dynamic Arrays Learn D's two array kinds: fixed-size static arrays and growable dynamic arrays, with length, append, concatenation, array-wise operations, multidimensional arrays and bounds checking.
- Slices in Depth: Views, Aliasing and Appending in D Understand that a D dynamic array is a slice (a pointer and a length), how slicing shares memory, when appending reallocates, and how to copy safely with dup and idup.
- Associative Arrays (Hash Maps) in D Use D's built-in hash maps: declare associative arrays, insert and look up keys safely with in and get, remove entries, iterate, count words, and choose suitable key types.
- Ranges and Algorithms in D: map, filter, sort and Lazy Pipelines Learn D's range concept (empty, front, popFront), build lazy pipelines with map, filter, take and iota, use sort, reduce and array, and write your own range.
Classes and Templates
- Classes and Objects in D Learn how D classes work: heap-allocated reference types, constructors, destructors, this, properties, const and final members, and how classes differ from structs.
- Inheritance and Interfaces in D Learn single inheritance in D: base classes, override, super, abstract classes, interfaces, virtual dispatch and casting with cast and typeid, with C vtable stand-ins.
- Templates and Generics in D Learn D templates: generic functions with type parameters, template structs and classes, value and alias parameters, specialization and implicit instantiation.
- Template Constraints and Type Checks in D Restrict D templates with if constraints, std.traits helpers, is expressions and static asserts so that bad type arguments fail early with clear errors.
Mixins and Metaprogramming
- String Mixins in D Learn D string mixins: how mixin(string) compiles generated source, builds code with CTFE, generates members and operators, and when to avoid them; with C X-macro stand-ins.
- Mixin Templates in D Learn D mixin templates: injecting fields and methods into classes and structs, parameterized mixins, name lookup rules and when they beat inheritance, with C macro stand-ins.
- Compile-Time Function Execution (CTFE) in D Learn D compile-time function execution: how ordinary functions run during compilation, enum and static initializers, lookup tables, limits, and a constexpr C stand-in.
- Traits, static if and Compile-Time Reflection in D Learn D compile-time reflection: __traits, static if, static foreach, version blocks and std.traits for inspecting types and members, with C _Generic and offsetof stand-ins.
Concurrency and Memory
- Parallelism with std.parallelism in D Learn data parallelism in D with std.parallelism: parallel foreach, taskPool.reduce, tasks and work units, plus races to avoid; C pthread stand-ins split work across threads.
- Message Passing with std.concurrency in D Learn message-passing concurrency in D: spawn, Tid, send, receive, receiveOnly, thread ownership and shutdown, with a C stand-in using a mutex-protected message queue.
- The Garbage Collector in D Understand D's garbage collector: how mark-and-sweep works, what allocates, GC.collect and GC.stats, @nogc code, tuning and avoiding pauses, with a runnable C mark-and-sweep simulator.
- scope Guards, RAII and Manual Memory in D Manage resources deterministically in D: scope(exit/success/failure), struct destructors for RAII, malloc and free, std.experimental.allocator and arenas, with a C cleanup stand-in.
Tooling and Interop
- dub, Modules and Packages in D Learn D modules and the dub build tool: module and import rules, package layout, dub.json and dub.sdl, dependencies, build types, and a C dependency-resolution stand-in.
- Unit Tests and Contracts in D Test D code with built-in unittest blocks and design by contract: assert, in and out contracts, class invariants, enforce, error versus exception, and a C test-runner stand-in.
- Interoperating with C and C++ from D Call C libraries from D and export D to C: extern(C) declarations, type mapping, strings and zero termination, callbacks, struct layout, importC and -betterC, with C-side demos.
Projects and Practice
- Project: A Tokenizer and Calculator in D Build a tokenizer, recursive-descent parser and evaluator for arithmetic in D, using ranges, algebraic enums and unit tests, with a runnable C version of the same design.
- Project: A Bank Account Simulator in D Build a bank account simulator in D with classes, interfaces, contracts, exceptions and unit tests, plus a runnable C version with a transaction log and state machine.
- D Practice Problems: Classes, Templates and Metaprogramming Twelve graded D exercises on classes, templates, mixins, compile-time code, concurrency and memory, with hints, D solution sketches and runnable C stand-in solutions.