Variables and D's Type System
D is statically and strongly typed: every variable has a fixed type determined at compile time, and the compiler rejects most type mismatches before the program ever runs. Unlike dynamically typed languages such as Python or JavaScript, you cannot reassign an int variable to hold a string later -- this catches entire classes of bugs at compile time rather than in production, at the cost of needing to declare (or infer) types up front.
Cricket analogy: Like a scorer who must record a delivery strictly as a run, wicket, or extra with no ambiguous entries, D's static typing forces every variable's category to be fixed and checked before the match (program) even starts, catching scoring errors before they happen.
Built-in Basic Types
D provides a full set of fixed-size numeric types: byte/ubyte (8-bit), short/ushort (16-bit), int/uint (32-bit), long/ulong (64-bit), plus float (32-bit), double (64-bit), and real (the largest floating-point type the hardware supports, often 80-bit on x86). Character types are separated by encoding: char holds a single UTF-8 code unit (8 bits), wchar a UTF-16 code unit (16 bits), and dchar a full UTF-32 code point (32 bits) -- this distinction matters because a single dchar always represents one whole Unicode character, while a char might only be part of a multi-byte sequence.
Cricket analogy: Like choosing between a T20 (short, fast format), an ODI (medium length), and a Test match (the longest format), D's byte, int, and long types are different match formats for numbers -- pick the smallest size that comfortably fits the range of values you expect.
import std.stdio;
void main() {
int age = 30; // 32-bit signed integer
long population = 8_000_000_000L; // 64-bit signed integer
double price = 19.99; // 64-bit floating point
bool isReady = true;
char initial = 'D'; // single UTF-8 code unit
dchar emoji = '😀'; // full UTF-32 code point
auto count = 42; // type inferred as int
const int maxRetries = 5; // this reference can't modify maxRetries
immutable double pi = 3.14159; // can never change, anywhere
writeln(age, " ", population, " ", price, " ", isReady);
}
Type Inference with auto, const, and immutable
The auto keyword lets the compiler infer a variable's type from its initializer, so auto count = 42; gives count type int without you writing it explicitly -- this is inference at compile time, not dynamic typing, since the type is still fixed forever afterward. D also has two flavors of "unchangeable": const, which means the variable can't be modified through this particular reference (but the underlying data might change through another reference), and immutable, which guarantees the data can never change through any reference for the lifetime of the program -- a stronger, transitive guarantee.
Cricket analogy: Like a third umpire inferring a batsman's exact position from replay footage without the batsman announcing it, D's auto infers a variable's type from its initializer automatically -- the type is still fixed, just deduced rather than stated aloud.
Arrays and Strings
D has three array kinds -- static arrays (int[5] a, fixed size on the stack), dynamic arrays (int[] a, heap-allocated, growable), and slices, which are lightweight views (pointer + length) into an existing array without copying data. Strings in D are simply arrays of characters: string is an alias for immutable(char)[] (UTF-8), so string literals are immutable by default and any attempt to mutate a string in place is a compile error -- you must build a new string or use char[]/dchar[] if you need mutability.
Cricket analogy: Like a fixed 11-player squad announced before the toss (static array, size locked) versus a dynamic bench that can call up reserves mid-tournament (dynamic array, growable), D's int[5] and int[] mirror these two squad management styles.
Because string is immutable(char)[], functions that need to build or append text often require a mutable char[] buffer or an Appender!string instead of a plain string. Also be careful when slicing a UTF-8 string by byte index -- since some characters take more than one byte in UTF-8, an arbitrary index can split a multi-byte character in half and produce invalid text.
- D is statically and strongly typed -- every variable's type is fixed at compile time and checked before the program runs.
- Numeric types scale from byte/ubyte (8-bit) up to long/ulong (64-bit), plus float, double, and real for floating point.
- char, wchar, and dchar hold UTF-8, UTF-16, and UTF-32 units respectively -- only dchar always represents one full Unicode character.
- auto infers a variable's type from its initializer at compile time; the type is still fixed, just not written explicitly.
- const means a value can't be changed through that specific reference; immutable means it can never change through any reference, ever.
- D has static arrays (fixed size), dynamic arrays (growable), and slices (lightweight views without copying).
- string is an alias for immutable(char)[], so string literals are immutable by default and can't be mutated in place.
Practice what you learned
1. What is the result of auto count = 42; in D?
2. Which type guarantees data can never change through ANY reference, for the program's whole lifetime?
3. What is the underlying alias for D's string type?
4. Which D array type does NOT copy the underlying data and is essentially a pointer-plus-length view?
5. Which character type always represents exactly one full Unicode code point?
Was this page helpful?
You May Also Like
What Is the D Programming Language?
An introduction to the D programming language -- its history, design philosophy, and the features that set it apart from C++, Java, and Go.
D Operators and Expressions
How D's arithmetic, comparison, logical, and special operators like ^^, .., and is work, with a focus on gotchas like integer division.
Your First D Program
Write, compile, and run your first D program, and understand the module, import, and main() concepts every D file relies on.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics