Metaprogramming
Metaprogramming is the practice of using programs or language mechanisms to inspect, generate, transform, or reason about other programs or their representations.
Functional Design and Architecture: Examples in Haskell
Metaprogramming is the manipulation or generation of program representations to produce or transform code, often during compilation.
- How it is used
- Uses compile-time code generation as a design tool for expressing code that would otherwise need to be written explicitly.
- How deep it goes
- Defines abstract-code manipulation and compile-time generation, then cautions that generated output can be difficult to inspect.
- What you can do after
- Evaluate metaprogramming as a design tool while accounting for the difficulty of inspecting generated code.
Read how Functional Design and Architecture: Examples in Haskell explains metaprogramming →
The Joy of JavaScript
Metaprogramming is the practice of writing programs that inspect, generate, or modify other programs or their own structure.
- How it is used
- Automates repetitive work and separates cross-cutting concerns from core application behavior.
- How deep it goes
- Explains symbols, Proxy, Reflect, and decorators as static hooks or dynamic weaving, without detailing their complete APIs.
- Examples
- Uses logging, tracing, performance metrics, iteration, primitive conversion, spreading, and printing as behavioral examples.
- What you can do after
- Apply it via Symbols, Proxy, Reflect, and decorators to alter behavior while preserving cleaner interfaces.
Write Powerful Rust Macros
Metaprogramming is the practice of writing code that treats other code as data so it can manipulate or generate code or add capabilities.
- How it is used
- Positions metaprogramming as a practical Rust technique for generating code, reducing duplication, and extending ordinary programming tools.
- How deep it goes
- Defines code generation and compile-time expansion, then distinguishes declarative and procedural macros without implementing either internally.
- Examples
- Uses Deserialize, format!, dbg!, test attributes, assert_eq!, and generated code hidden within a small Rust program.
- What you can do after
- Evaluate/understand when Rust macros provide capabilities that functions and ordinary coding tools cannot provide.
Read how Write Powerful Rust Macros explains metaprogramming →
Also covered in
- Deep Learning with R, Third Edition
- Systems Programming with ZigIn early access (MEAP)
- Julia as a Second Language
- Idiomatic Rust
- F# in Action: A Revised Edition of Get Programming with F#
- The Well-Grounded Rubyist, Fourth Edition
- Code Like a Pro in Rust
- Clojure: The Essential Reference
What to know first
- templateA C++ template is a parameterized declaration or definition from which the compiler instantiates functions, classes, or other code for specified types or values.
- domain-specific languageA domain-specific language is a language or notation tailored to expressing concepts, operations, or solutions within a particular problem or application domain.
- macroA macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
- pattern matchingPattern matching is the process of comparing values or input with specified patterns to identify matches, classify data, or select corresponding behavior.
Related concepts
- comptimeuses · Comptime is Zig's compile-time execution feature that allows code to be evaluated during compilation.
- callbackuses · A callback is a function or other callable supplied or registered with a component for invocation at a specified event, condition, or point in execution.
- macroincludes · A macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
- reflectionuses · Reflection is a programming-language or runtime capability that lets a program inspect or manipulate its own types, structure, values, or metadata during execution.
Questions about metaprogramming
What is metaprogramming?
Metaprogramming is the practice of using programs or language mechanisms to inspect, generate, transform, or reason about other programs or their representations.
Which Manning books explain metaprogramming?
Functional Design and Architecture: Examples in Haskell, The Joy of JavaScript and Write Powerful Rust Macros.
What should I know before learning metaprogramming?
Manning books that teach metaprogramming usually introduce template, domain-specific language, macro and pattern matching first.
What kinds of metaprogramming are there?
Kinds of metaprogramming include macro and template metaprogramming.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


