Practical Generics in Swift
summary
Claim: generics cut duplicate code while keeping Swift’s strong type safety. Specific demos show a swapping function generalized into a type-parameter, a Stack<T> struct with protocol constraints, a Result<T> enum for network success/error handling, and a generic data source driving two different table views from one class. Code uses Swift (Swift 3 features like type parameters), the sample app builds and runs, and the examples behave as shown without runtime crashes.
AI-written summary · about
chapters (15)
- 0:00Start
- 1:49Have we ever had anything like Generics?
- 3:13The Book Example - Swapping Values
- 10:48A Generic Stack Data Structure
- 12:02The Code for a Simple Generic Stack
- 13:09Using the Stack
- 14:08Constraining Generic Types
- 15:38Creating and Adopting a Protocol
- 16:49Constraints in Action
- 18:15A Standard Network Request
- 21:17Network Request with Completion Handler
- 23:21Network Request with Result Enum
- 25:19Processing the Result Response
- 25:50Handling the Error cases with Result
- 26:52Handling the Success case with Result
related talks
16mGenerics in Theory and Practice
15mMaking it Generic
20mGenerics in protocol extensions
23mDeclarative Programming in Swift
20mHow Not to Write Swift
48mLearning From Our Elders: Applying functional insights without losing Swift
34mSwift: New Paradigms for iOS Development
27mGeneric Protocols and Associated Types - Power Tools for Unit Testing
