---
title: "A Kotlin Style .copy Function for Swift Structs"
description: "Swift structs lack Kotlin data class's copy function, so mimicking immutable state updates requires boilerplate."
url: "https://swe.quest/articles/a-kotlin-style-copy-function-for-swift-structs-77daf3d8ba10bce5"
canonical: "https://shopify.engineering/kotlin-style-copy-function-swift-structs"
source: "Shopify Engineering"
published: "2021-09-02"
difficulty: "medium"
kind: "How-to"
reading_minutes: 4
tags: ["swift","mobile","language-design"]
---

# A Kotlin Style .copy Function for Swift Structs

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2021-09-02, 4 min read, How-to. Original: [https://shopify.engineering/kotlin-style-copy-function-swift-structs](https://shopify.engineering/kotlin-style-copy-function-swift-structs)

## Summary

Swift structs lack Kotlin data class's copy function, so mimicking immutable state updates requires boilerplate. A memberwise init plus a copy function with optional nil-defaulted params works until optional properties need to be set back to nil, and Swift can't reference self in default parameter values. The workaround is a functional builder: a closure receives a builder seeded with the original values and overrides selected fields. A Sourcery stencil template generates the init and builder automatically.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Swift](https://swe.quest/tags/swift.md) (Languages)
- [Mobile](https://swe.quest/tags/mobile.md) (Web & mobile)
- [Language design](https://swe.quest/tags/language-design.md) (Compilers & runtimes)

## Prerequisites

- Swift structs ([easy posts on Swift](https://swe.quest/tags/swift.md?difficulty=easy))
- Kotlin data classes ([easy posts on Java & JVM](https://swe.quest/tags/jvm.md?difficulty=easy))

## Keywords

Swift, Kotlin, Sourcery, data class, builder pattern

## Related posts

- [What's new in Swift: September 2026 Edition](https://swe.quest/articles/what-s-new-in-swift-september-2026-edition-c33a1cfebf7ae281.md): Swift.org · 2026-10-08 · Easy · 4 min read · Swift, WebAssembly, Build systems
  Swift 6.4 shipped with Swift Build as the default SwiftPM build system, Subprocess 1.0, up to 40x faster WebAssembly bridging via JavaScriptKit, path-based module tracking in debug info (build systems should switch to \`-debug-module-path\`), and Embedded Swift improvements.
- [Swift 6.4 Released](https://swe.quest/articles/swift-6-4-released-393cd350ed7f57b9.md): Swift.org · 2026-09-15 · Medium · 7 min read · Swift, Language design, Build systems, WebAssembly, Memory management
  Swift 6.4 makes Swift Build the default backend for SwiftPM, giving consistent builds across Linux, macOS and Windows, adds SBOM generation, and ships Subprocess 1.0 for cross-platform process execution on Swift concurrency.
- [How We Built The Deals Tab in Swift UI](https://swe.quest/articles/how-we-built-the-deals-tab-in-swift-ui-81d29cc7e9699bbd.md): Etsy Code as Craft · 2024-03-05 · Medium · 13 min read · Swift, Mobile, Refactoring & migrations, Build systems, Developer productivity
  Etsy rebuilt its mobile Updates tab as a new Deals tab in SwiftUI inside a modular Tuist module, which made preview builds nearly instant and let the team build modules from mock preview data about two weeks before the APIs were ready.
- [Swift 6.3 Released](https://swe.quest/articles/swift-6-3-released-7c44208b49d0d9e6.md): Swift.org · 2026-03-24 · Medium · 6 min read · Swift, Build systems, Testing, Mobile, Documentation
  Swift 6.3 adds a \`@c\` attribute for exposing Swift functions and enums to C (or implementing C-declared functions in Swift via \`@c @implementation\`), \`Module::name\` selectors for disambiguating APIs from different imported modules, and library performance attributes \`@specialize\`, \`@inline(always)\` and \`@export(implementation)\`.
- [Oxidizing OCaml: Rust-Style Ownership](https://swe.quest/articles/oxidizing-ocaml-rust-style-ownership-5670c1001a0b9cc2.md): Jane Street Tech Blog · 2023-06-21 · Hard · 14 min read · Functional programming, Type systems, Language design, Memory management, Rust
  Jane Street's OCaml extensions add Rust-style ownership through mode axes.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
