liveBook · Manning

Metaprogramming

As explained in Functional Design and Architecture: Examples in Haskell

Metaprogramming is the manipulation or generation of program representations to produce or transform code, often during compilation.

Metaprogramming manipulates abstract code declarations so a compiler can generate additional code at compile time. It can make a codebase more expressive and flexible, but generated code is implicitly embedded and may be hard to inspect. section 2.1.2

Use it carefully when repeated structural code obscures a design. For example, generated lenses can remove accessor boilerplate for nested data while preserving concise, readable operations. section 2.1.2

Generating lenses for a record

A Person record declares private-looking fields, then makeLenses generates lenses used by getFirstName. The call expresses the desired generated interface rather than manually writing each accessor. section 2.1.2

data Person = Person { _name :: PersonName       #1
                     , _age :: Age }
  
person = Person ("Jane", "Doe") 31
 
makeLenses ''Person                              #2
  
getFirstName :: Person -> String
getFirstName person = view (name . _1) person    #3
  
-- getFirstName janeDoe == "Jane"

Inspect the record declaration, generation instruction, and resulting lens composition together. section 2.1.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines compile-time code generation and its visibility trade-off without detailing compiler mechanics.
Examples
Illustrates generated Person lenses through makeLenses and composed field access.
What you can do after
Evaluate/understand when compile-time generation can improve expressiveness while obscuring emitted code.
How it connects
Connects metaprogramming with lenses, domain-specific languages, abstract data types, and functional idioms.

Also explained in

Compare how 3 books explain metaprogramming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.