Copyable

1.0.0

Copyable is a Swift Macro used to bring Kotlin's `copy` functionality to Swift.
hootsuite/copyable-macro

What's New

v1.0.0

2025-01-28T16:45:18Z

What's Changed

New Contributors

Full Changelog: https://github.com/hootsuite/copyable-macro/commits/v1.0.0

copyable-macro

Inspired by the blog by Scott Birksted, Copyable is a Swift Macro used to bring Kotlin's copy functionality on data classes to Swift's structs.

Functionality

Creates a new instance of the struct with all properties copied from the original, allowing selective modification of specific properties while keeping others unchanged

Swift Package Manager

In Package.swift:

dependencies: [
    .package(url: "https://github.com/hootsuite/copyable-macro.git", from: "1.0.0")
]

Usage

import Copyable

@Copyable
struct Student {
    let name: String
    let grade: Int
}

let student1 = Student(name: "Matthew", grade: 100)

print("name: \(student1.name) grade: \(student1.grade))

This should print: "name: Matthew grade: 100"

let student 2 = student1.copy { student in  
    student.name = "Henry"
}

print("name: \(student2.name) grade: \(student2.grade))

This should print: "name: Henry grade: 100"

Reference

A Kotlin Style .copy Function for Swift Structs

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

Last updated: Wed May 14 2025 18:52:02 GMT-0900 (Hawaii-Aleutian Daylight Time)