AgentSkillsCN

m05-type-driven

精通 C++ 类型驱动设计。触发点:强类型、幻影类型、类型状态模式、构建器模式、无效状态不可表示。

SKILL.md
--- frontmatter
name: m05-type-driven
description: "Mastering C++ Type-Driven Design. Triggers: strong types, phantom types, type state pattern, builder pattern, invalid state unrepresentable."

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.