Shopify Scott Birksted

Shopify's Kotlin-Style Copy Function For Swift Structs

Original post Kotlin-Style Copy Function For Swift Structs

Article Summary

Scott Birksted from Shopify tackles a frustrating gap between Kotlin and Swift: why can't Swift structs copy themselves as elegantly as Kotlin data classes?

When Shopify's mobile team tried bringing Kotlin's immutable state pattern to iOS, they hit a wall. Swift structs lack the convenient .copy() function that Kotlin developers take for granted. This article walks through the evolution of a solution, from basic workarounds to an automated approach using code generation.

Key Takeaways

Critical Insight

A builder pattern combined with Sourcery code generation brings Kotlin-style immutable state management to Swift without manual boilerplate.

Continue reading

What This Article Covers

Problem

Swift structs don't have Kotlin's .copy() function, so developers end up writing a lot of boilerplate code when they need to create modified copies of immutable state objects. This is especially tedious when working across both iOS and Android codebases.

Solution

Scott Birksted's team built a functional builder pattern using closures to capture property overrides. They then automated the whole thing with Sourcery code generation, using a reusable stencil template to handle the repetitive work.

Impact

Any struct marked with @AutoCopy now gets initializers and builder structs generated automatically by the Sourcery template. This cuts out the manual boilerplate while keeping the same immutability patterns that Kotlin developers are used to.

The article includes a complete Sourcery template that generates the entire copy mechanism automatically for any struct you annotate.

This page summarizes “Kotlin-Style Copy Function For Swift Structs” by Shopify, published Sep 2, 2021. All credit for the original work goes to the authors.

Read the original on shopify.engineering (opens in new tab)

AI tools for iOS

Browse all AI tools →