{"id":31913790,"url":"https://github.com/wilhelmoks/modifiedcopymacro","last_synced_at":"2025-10-13T18:52:50.884Z","repository":{"id":178348227,"uuid":"661742374","full_name":"WilhelmOks/ModifiedCopyMacro","owner":"WilhelmOks","description":"A Swift macro for making inline copies of a struct by modifying a property.","archived":false,"fork":false,"pushed_at":"2025-07-21T08:28:28.000Z","size":27,"stargazers_count":33,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-18T19:31:21.664Z","etag":null,"topics":["copy","inline","macro","struct","swift","value-types"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WilhelmOks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-03T14:40:26.000Z","updated_at":"2025-07-21T08:23:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"271bfca9-737f-42a0-a510-cbe0b9dd1f10","html_url":"https://github.com/WilhelmOks/ModifiedCopyMacro","commit_stats":null,"previous_names":["wilhelmoks/modifiedcopymacro"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/WilhelmOks/ModifiedCopyMacro","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilhelmOks%2FModifiedCopyMacro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilhelmOks%2FModifiedCopyMacro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilhelmOks%2FModifiedCopyMacro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilhelmOks%2FModifiedCopyMacro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WilhelmOks","download_url":"https://codeload.github.com/WilhelmOks/ModifiedCopyMacro/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilhelmOks%2FModifiedCopyMacro/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279016625,"owners_count":26085853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["copy","inline","macro","struct","swift","value-types"],"created_at":"2025-10-13T18:50:41.181Z","updated_at":"2025-10-13T18:52:50.878Z","avatar_url":"https://github.com/WilhelmOks.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Swift-5.9-orange.svg\" /\u003e\n    \u003ca href=\"https://swift.org/package-manager\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003c/a\u003e\n    \u003cimg src=\"https://img.shields.io/badge/platforms-macOS | iOS | tvOS | watchOS | Linux-brightgreen.svg?style=flat\" alt=\"Platforms: macOS, iOS, tvOS, watchOS, Linux\" /\u003e\n\u003c/p\u003e\n\n# ModifiedCopyMacro\nA Swift macro for making inline copies of a struct by modifying a property.\u003cbr/\u003e\nThe syntax is similar to Kotlin's copy function for data classes: https://kotlinlang.org/docs/data-classes.html#copying\n\n## Usage\n\nApply the `@Copyable` macro to a struct:\n\n```swift\n@Copyable\nstruct Person {\n    let name: String\n    var age: Int\n}\n```\n\nand it will add a copy function for each stored property and constant:\n```swift\nstruct Person {\n    let name: String\n    var age: Int\n\n    /// Returns a copy of the caller whose value for `name` is different.\n    func copy(name: String) -\u003e Self {\n        .init(name: name, age: age)\n    }\n    \n    /// Returns a copy of the caller whose value for `age` is different.\n    func copy(age: Int) -\u003e Self {\n        .init(name: name, age: age)\n    }\n}\n```\n\n## Capabilities, Limitations and Design Choices\n\n### Chains for multiple changes\n\nTo make a copy of a struct and modify multiple properties, you can chain the `copy` calls like this:\u003cbr/\u003e\n`Person(name: \"Walter White\", age: 50).copy(age: 52).copy(name: \"Heisenberg\")`\u003cbr/\u003e\n\nThis is different than Kotlin's version of `copy`, which allows multiple parameters to pass in a single call.\u003cbr/\u003e\nIt's not possible to implement it like that in Swift because it's not possible to have default values for parameters which refer to the current values of the properties of the struct (or class).\u003cbr/\u003e\nAnd we also can't use nil as a marker for the old/current value, because nil might be a valid new value that we want the property to set to when we make a copy.\u003cbr/\u003e\nThere might be a way that I'm not aware of, to still make it possible. So if you know how to do it, please let me know.\u003cbr/\u003e\n\n#### CopyableCombi\n\nWith version 2.1.0, the separate macro CopyableCombi was introduced, which generates copy functions with all combinations of parameters.\nThis solution has the disadvantage that the number of generated functions can become large quickly, but it provides an API which is more similar to Kotlin's copy function.\n\n### Stored properties and constants\n\nA copy function will be generated for each stored property (`var`) and each constant (`let`) of the struct.\u003cbr/\u003e\nThe macro recognizes computed properties by checking if they have `get` or `set` accessors.\u003cbr/\u003e\n\n### Only for struct\n\nThis macro works only for structs.\u003cbr/\u003e\nIt doesn't make sense for enums because enums can't have stored properties.\u003cbr/\u003e\nClasses and actors have reference semantics and I don't want this library to provide a copy function for reference types. I just want to augment the natural copy capability of structs with modified properties.\u003cbr/\u003e\nThis macro emits a Diagnostic Message when you try to apply it to anything but a struct.\u003cbr/\u003e\n\n### Only with default init\n\nThe generated copy functions produce the copies by calling the synthesized default initializer.\u003cbr/\u003e\nSo, if you provide a custom initializer, no default initializer will be synthesized and the copy functions won't work.\u003cbr/\u003e\nYou can still have synthesized default initializers if you define your custom initializers in an extension.\u003cbr/\u003e\n\n### No struct extensions\n\nYou can't apply this macro on an extension of a struct.\u003cbr/\u003e\nThis seems to be a limitation of the macro system.\u003cbr/\u003e\nIf you know how to make it possible, please let me know :)\n\n## Installation\n\nMacros are a new language feature of Swift 5.9 and it will only work in Xcode 15 and later.\n\nAdd the url `https://github.com/WilhelmOks/ModifiedCopyMacro.git` as a Swift Package to your project.\n\nWhen prompted, select the Package Product `ModifiedCopy` (Kind: Library) to add to your target.\n\nMake a clean build.\n\n`import ModifiedCopy` in the file where you want to attach the `@Copyable` macro.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilhelmoks%2Fmodifiedcopymacro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilhelmoks%2Fmodifiedcopymacro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilhelmoks%2Fmodifiedcopymacro/lists"}