agentleFS
13eholder13eholder/Modern-Cpp-Skills/m05-type-driven/SKILL.md

m05-type-driven

Skill14 starsChanged 9 months agoMITView on GitHub

Mastering C++ Type-Driven Design. Triggers: strong types, phantom types, type state pattern, builder pattern, invalid state unrepresentable.

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

C++ Type-Driven Design

Core Question

Can I make this bug a compile error?

  • Primitive Obsession: Using int for IDs, double for Money. Bad.
  • Strong Types: struct UserId, struct Money. Good.
  • Type State: Connection<OFF> vs Connection<ON>.

Error → Design Question

IssueDesign Question
Swapped argumentsDid you pass width to height? (Use Strong Types).
Invalid StateDid you call read() on closed file? (Use Type State).
Unit confusionDid you mix Meters and Feet? (Use std::chrono style units).

Thinking Prompt

  1. Is this int unique?
    • Yes? → Wrap in struct.
    • struct UserId { int val; }; prevents process(OrderId).
  2. Does valid usage depend on order?
    • Yes? → Encode state in type.
    • Builder::port() returns BuilderWithPort.
  3. Are units compatible?
    • No? → Template tag. Dist<Meters> + Dist<Feet>.

Trace Up / Down

  • Trace Up:
    • Issue: "Rocket crashed because of Metric vs Imperial confusion."
    • Cause: double calculate_trajectory(double dist) accepted any number.
    • Fix: Dist<Meters> calculate(Dist<Meters> d). Compilation fails if you pass Feet.
  • Trace Down:
    • Intent: "Ensure file is open before reading."
    • Code: File<Open> f = File<Closed>().open(); f.read();

Quick Reference

PatternCostUse When
Struct WrapperZeroDistinct IDs, coordinates.
Enum ClassZeroType-safe flags (no implicit int conv).
Phantom TypeZeroTracking state without storage.
User LiteralZero10_m, 50_s.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.