The Expression Problem and Rust
21 hours ago
- #Rust
- #Expression Problem
- #Software Design
- The expression problem in software design involves balancing extensibility between adding new data types and new operations.
- Rust's trait objects initially seem to avoid the expression problem, but the solution is not straightforward.
- Two common approaches in Rust are enum-based (easy to add operations, hard to add data types) and trait-based (easy to add data types, hard to add operations).
- Splitting a single trait into multiple traits per operation was proposed as a solution but introduces new issues, such as hard-coding operations in data type definitions.
- A proposed solution involves using generics to allow functions to specify required operations or data types dynamically, avoiding hard-coded lists.
- Implementing this solution in Rust requires complex patterns, such as using `Unsize` for trait objects, which may be overengineering for many use cases.
- The article suggests that sometimes committing to opinionated design choices is better than striving for perfect extensibility in statically typed languages like Rust.