Memberwise Initializer in Swift explained with Code Examples
Article Summary
Antoine van der Lee explores a subtle Swift 6.4 change that eliminates a common source of boilerplate code. If you've ever written manual initializers just to hide private properties, this one's for you.
Swift's memberwise initializers automatically generate struct initialization code, but before Swift 6.4, private properties with default values could accidentally make the entire initializer private. This forced developers to write manual boilerplate just to work around access control quirks.
Key Takeaways
- Swift 6.4 excludes private initialized properties from memberwise initializers automatically
- Before 6.4, private properties forced entire initializers to become private
- Change aligns with SE-0502 and helps macro authors avoid backing storage issues
- Public structs still require explicit public initializers in all Swift versions
Critical Insight
Swift 6.4 makes memberwise initializers smarter by treating private properties with defaults as implementation details, removing the need for manual initializer boilerplate.