A Kotlin Style .copy Function for Swift Structs

Swift’s struct isn’t as convenient as Kotlin’s data class because it doesn't have a similar copy function. Learn how to adopt this pattern in Swift.

Scott Birksted
4 min readbeginner
--
View Original

Overview

The article discusses how to implement a Kotlin-style .copy function for Swift structs to facilitate immutability in iOS development. It highlights the challenges of Swift's lack of a built-in copy function and presents solutions, including a functional builder pattern and using the Sourcery code generator to automate boilerplate code.

What You'll Learn

1

How to create an initializer for a Swift struct that captures all field values

2

Why using a functional builder pattern can simplify property overrides in Swift structs

3

How to automate boilerplate code for Swift structs using Sourcery

Prerequisites & Requirements

  • Understanding of Swift structs and immutability concepts
  • Familiarity with Sourcery code generator(optional)

Key Questions Answered

How can you implement a copy function for Swift structs?
To implement a copy function for Swift structs, you can define an initializer that captures all properties and a .copy function that allows for optional overrides. This approach helps maintain immutability while simplifying the process of creating modified copies of structs.
What challenges arise when using optional properties in Swift structs?
When using optional properties in Swift structs, the default behavior of a copy function can lead to issues where setting a parameter to nil does not change the value. This necessitates alternative solutions, such as using a builder pattern to manage property overrides effectively.
What is the functional builder pattern and how is it applied in Swift?
The functional builder pattern allows you to create a new struct instance by passing a closure that specifies which properties to override. This method simplifies the copying process and makes it easier to manage changes without extensive boilerplate code.

Technologies & Tools

Tools
Sourcery
Used to automate the generation of boilerplate code for Swift structs.

Key Actionable Insights

1
Implement a custom initializer for your Swift structs to ensure immutability while allowing for easy property overrides.
This approach is essential when you want to maintain the integrity of your data models while still enabling updates to specific fields, similar to Kotlin's data classes.
2
Utilize the functional builder pattern to streamline the process of creating modified copies of structs in Swift.
This method reduces boilerplate code and enhances readability, making it easier to manage complex data structures with multiple properties.
3
Leverage Sourcery to automate the generation of boilerplate code for Swift structs.
By using Sourcery, you can save time and reduce errors in your codebase, allowing you to focus on more critical aspects of your application development.

Common Pitfalls

1
Forgetting to define an initializer that captures all properties can lead to issues when trying to create copies of structs.
This oversight can result in losing the default initializer provided by the compiler, complicating the copying process and requiring additional boilerplate code.
2
Not handling optional properties correctly in the copy function can lead to unintended behavior when setting values to nil.
This can cause confusion and bugs, as the copy function may not behave as expected if it defaults to the current value instead of allowing for nil.

Related Concepts

Immutability In Programming
Functional Programming Patterns
Code Generation Tools In Swift