D from the ground up
The speed of C with the comfort of a modern language
- Beginner
- 12 chapters
- 198 min read
A hands-on introduction to D: one main function and writeln, values in int, double, string and bool, auto as the compiler naming the type, immutable and const and what each promise costs, text with std.string and to!string, decisions with if and switch, ranges with foreach, functions with defaults and ref, arrays and slices with std.algorithm, associative arrays with sorted keys, structs with methods and toString, classes and exceptions, a first template, and a capstone that sorts records into a formatted score report. Every idea runs on a real compiler.
What You'll Learn
- Write and run a D program, and say what void main and import do
- Name values with int, double, string, bool and char, and let auto infer a type
- Use immutable, const and enum, and explain what each one promises
- Measure, cut and rebuild text, and convert values with to!string
- Choose a path with if and switch, and repeat work with foreach over a range
- Write functions with default arguments, ref parameters and overloads
- Grow an array, slice it with the dollar sign, and use std.algorithm on it
- Count and look up values in an associative array, printed in key order
- Model a record as a struct with methods, toString and an immutable form
- Throw and catch an exception, with an error class of your own
- Write a function template once and let the compiler instantiate it per type
- Assemble a report from records, a sort rule and formatted numbers
Prerequisites
- No D at all — the course starts at the first program
- Skill with any other compiled language makes the early chapters quicker, but none is assumed
Course Content
- 01Your first D programNot started
What D is for, what the compiler needs before anything runs, and the smallest complete program: one main function, one import, and a few lines of output.
- 02Types, values and autoNot started
The five types you will use every day, what auto really means, and the one place where whole numbers and fractions disagree.
- 03immutable, const and enumNot started
Three ways to promise that a value will not change, what each promise costs, and why the compiler takes them more seriously than a comment.
- 04Text: strings, slices and to!stringNot started
Measuring and cutting text, the verbs std.string and std.array provide, and the conversion that turns any value into a string and a string back into a number.
- 05Decisions and loopsNot started
Choosing a path with if and switch, walking a range with foreach, and the rule that makes a D condition different from the same condition in C.
- 06Functions, defaults and refNot started
Naming a piece of work, letting a caller leave an argument out, changing a value through ref, and letting one name cover two kinds of argument.
- 07Arrays, slices and algorithmsNot started
Growing a list, cutting a window out of it with the dollar sign, copying before sorting, and the three std.algorithm calls that replace most loops.
- 08Associative arraysNot started
Looking a value up by key, counting occurrences without writing a search, the difference between asking with in and reading a key directly, and why the keys need sorting before they are printed.
- 09Structs with methods and toStringNot started
Gathering related values into a type of your own, giving that type behaviour, teaching it to describe itself, and making a finished record immutable.
- 10Classes and exceptionsNot started
The one place this course uses a class, how throw moves control to a catch, and how to give a failure its own type and message.
- 11Templates: one function, many typesNot started
Writing a function once and letting the compiler make a version per type, reading the exclamation mark in code you have already used, and what an error inside a template looks like.
- 12Capstone: a score reportNot started
A program that uses the whole course: records in a struct, an array of them, a sort with your own rule, aligned columns, and a total with one decimal place.
