Macros vs Types
summary
Argues that neither wins alone: combine strong compile‑time types with compile‑time code emission via Scala macros to get readable APIs and fast generated code. Demonstrates macro inlining for arithmetic that removes millions of allocations, implicit macro materialization of type‑class instances via shapeless, statically sized collections using implicits, and an overflow‑checking macro that returns None on detected overflow. Shows three Slick DSL approaches and a yinyang shadow embedding that preserves meaningful types. Works in practice but macros are opaque, slow to debug, and can blow up compile time (type‑level examples slow dramatically around 22→23).
AI-written summary · about
chapters (24)
- 0:00Start
- 0:29Macros vs Types
- 2:06Textual code generation
- 2:20Textual codegen is too low-tech
- 3:23Enter macros
- 4:44Use case: Spire Ops
- 7:04Use case: Materialization
- 9:29Type classes à la Scala
- 11:36The dangers of materialization.
- 14:42Type computations
- 16:11Shapeless
- 21:14Let's overthrow the tyranny of types!
- 22:27Reasonable use case: Checked arithmetics
- 23:08Reasonable use case: WartRemover
- 24:31Domain-specific languages
- 24:45Use case: Slick
- 25:48Lifted embedding (types)
- 27:54Direct embedding (macros)
- 29:36Shadow embedding (macros + types)
- 31:13Our advice
- 32:48Types are more declarative, but less powerful
- 33:05Macros are more powerful, but less declarative
- 33:42Embrace reason, use whatever's simpler
- 34:55Three approaches
related talks
42mAvoiding macros with inline and derived
43mAdventures in Metaprogramming: Macros versus Shapeless
39mPhilosophy of Scala macros
27mDo You Even Macro?
29mADVENTURES IN META-PROGRAMMING
48mMetaprogramming in Dotty
17mHerding types with Scala macros
43mIt quacks but it’s not a duck structural typing in Scala 3
