Designing APIs for Value Semantics
Choosing struct vs class when designing public interfaces and frameworks.
Welcome
Designing APIs around value semantics leads to predictable, thread-safe, and easy-to-test code. Let's examine the principles and patterns for making that choice.
The Value Semantics Contract
A type has value semantics if:
• Two independent copies are forever independent
• Mutating one copy never affects the other
• The type behaves like `Int` — `a = b; a += 1` leaves `b` unchanged
All lessons in this course
- Stack vs Heap: Where Values Live
- Mutation, Sharing and Unexpected Aliasing
- Mixed Value/Reference Graphs
- Designing APIs for Value Semantics