Typestate – the new Design Pattern in Swift 5.9
This talk will teach the audience 3 ideas:
1. Typestate Design Pattern, popular in Rust, and now available in Swift 5.9.
2. Swift 5.9’s new Noncopyable types and consuming functions.
3. The power of generic constraints.
Talk outline:
Typestate Design Pattern.
– Understand how Typestate brings the concept of a State Machine into the type system, catching serious logic mistakes at compile time.
– Explore its application in mission-critical systems, with a real-world example of a Tesla car, highlighting its importance in safety-critical scenarios, .
Swift 5.9’s New Noncopyable Types.
– Discover Swift 5.9’s strict memory ownership model and the Noncopyable types.
– Understand how consuming functions prevent the reuse of state variables, ensuring compiler-enforced state transitions
The Power of Generic Constraints.
– Learn how generic constraints allow us to enforce state-specific operations at the type level, eliminating the need for runtime guard
Practical examples:
– Coin-operated turnstile: Walk through the step-by-step implementation of a Turnstile using the Typestate pattern in Swift.
– Playing Video games on a Tesla Car: Explore the example of how a Tesla car can use the real steering wheel and pedals to either control the physical car or a video game car, depending on its state. Highlighting the safety guarantees that Typestate provides in high-stakes
Conclusion and Takeaways:
– Go through the checklist to evaluate how the Typestate Design Pattern fits into your projects, especially if you deal with state machines, require a strict order of operations, or need to manage resources.
– Recap how Typestate helps prevent serious errors without relying heavily on traditional quality control measures such as tests, linters, code review, etc.
Session info:
Speaker: Alex Ozun
Senior iOS Engineer @ American Express at American Express
Date: 15 March 2024
Time: 13:15 - 14:00
Relevant tags:
iOS
Swift