### Quick Start Commands Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Basic commands for running tests, formatting code, and cleaning build artifacts. ```bash make test # Run tests locally make format # Format code make clean # Clean build artifacts ``` -------------------------------- ### Podman Setup Script Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Commands for setting up and managing the Podman environment for testing. ```bash # First time, or to reconfigure bin/setup-podman # Quick start (non-interactive) bin/setup-podman --auto # Check status bin/setup-podman --status ``` -------------------------------- ### Macro-Level Configuration Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Example of configuring the @MemberwiseInit macro with different parameters. ```swift @MemberwiseInit(.public) @MemberwiseInit(_deunderscoreParameters: true) @MemberwiseInit(_optionalsDefaultNil: true) ``` -------------------------------- ### Type Inference Examples Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Examples demonstrating type inference from property initializers. ```swift var count = 0 // Int var items = [Int]() // [Int] var mapping = [:] // Can't infer; annotation required let x = 1.0..<5.0 // Range ``` -------------------------------- ### Label-less/Wildcard Parameters Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example demonstrating how to create label-less parameters using @Init(label: "_"). ```swift @MemberwiseInit struct Point2D { @Init(label: "_") let x: Int @Init(label: "_") let y: Int } ``` ```swift init( _ x: Int, _ y: Int ) { self.x = x self.y = y } ``` -------------------------------- ### @InitWrapper Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @InitWrapper for property wrappers like @Binding. ```swift @InitWrapper(type: Binding.self) @Binding var count: Int ``` -------------------------------- ### Full Configuration Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md An extensive example showcasing various configuration options including access level, de-underscore parameters, default nil for optionals, custom labels, ignored properties, and wrapper types. ```swift @MemberwiseInit( .public, _deunderscoreParameters: true, _optionalsDefaultNil: true ) public struct User { public let id: String @Init( .public, label: "email_address", default: nil ) private var _email: String? @Init(.ignore) private var _internalCache: [String: Any] = [:] @InitWrapper( type: Binding.self, label: "age_binding" ) @Binding var _age: Int } // Expands to (approximately): // public init( // id: String, // email_address _email: String? = nil, // age_binding _age: Binding // ) { // self.id = id // self._email = _email // self._age = _age // } ``` -------------------------------- ### Advanced Usage of test-linux script Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Examples of advanced options for the ./bin/test-linux script, including dry runs, specific combination testing, parallelism control, logging, debugging, and sequential execution. ```bash # Preview without running ./bin/test-linux --dry-run # Test specific combinations ./bin/test-linux --swift 6.0 --swift-syntax 600 ./bin/test-linux --swift 6 # All Swift 6.x versions # Override auto-detected parallelism ./bin/test-linux --parallel 4 --continue-on-error # Save logs for analysis ./bin/test-linux --parallel --continue-on-error --log-dir ./logs # Debug a specific failure ./bin/test-linux --swift 6.2 --swift-syntax 601 --verbose # Run sequentially (no parallelism) ./bin/test-linux --sequential ``` -------------------------------- ### Basic Property Inclusion Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md A basic example showing how `@MemberwiseInit` includes properties, even those with `@State`. ```swift ```swift import SwiftUI import MemberwiseInit @MemberwiseInit struct MyView: View { // Normally ignored due to @State attribute @Init @State var isVisible: Bool = false var body: some View { /* ... */ } } // Generated init includes the isVisible parameter // init(isVisible: Bool = false) ``` ``` -------------------------------- ### CounterView Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example of using @InitWrapper with a Binding property. ```swift @MemberwiseInit struct CounterView: View { @InitWrapper(type: Binding.self) @Binding var isOn: Bool var body: some View { … } } ``` -------------------------------- ### Multiple Configurations Error Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example showing the error when a single property has multiple @Init attributes. ```swift @MemberwiseInit struct Example { @Init(.public) @Init(.public) let value: String } // Error: Multiple @Init configurations are not supported by @MemberwiseInit ``` -------------------------------- ### Experimental @_UncheckedMemberwiseInit Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md An example demonstrating the experimental @_UncheckedMemberwiseInit macro, which bypasses safety checks. ```swift @_UncheckedMemberwiseInit(.public) public struct APIResponse: Codable { public let id: String @Monitored internal var statusCode: Int private var rawResponse: Data // Computed properties and methods... } ``` -------------------------------- ### Macro-Level Configuration Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Example demonstrating how macro-level configuration applies to all properties within a struct. ```swift @MemberwiseInit(.public, _optionalsDefaultNil: true) struct Type { let x: String? // Uses macro-level settings let y: Int? // Uses macro-level settings } ``` -------------------------------- ### Custom Parameter Labels Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example showing how to assign custom parameter labels using @Init(label: String). ```swift @MemberwiseInit struct Receipt { @Init(label: "for") let item: String } ``` ```swift init( for item: String // 👈 ) { self.item = item } ``` -------------------------------- ### SwiftUI Views Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit with SwiftUI Views, including @State and @Binding properties. ```swift import SwiftUI @MemberwiseInit struct CounterView: View { @Init @State var count: Int = 0 @InitWrapper(type: Binding.self) @Binding var isEnabled: Bool var body: some View { ... } } ``` -------------------------------- ### SwiftUI Component Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit for a SwiftUI View component. ```swift @MemberwiseInit struct MyButton: View { let label: String @Init @State var isHovered: Bool = false let action: () -> Void } ``` -------------------------------- ### Troubleshooting Podman Not Running Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Command to restart Podman if it is not running. ```bash bin/setup-podman --auto ``` -------------------------------- ### Testing Helper Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit for a testing helper class. ```swift @MemberwiseInit class TestDouble { let id: String private var _mockState: MockState } ``` -------------------------------- ### Access Level Configuration Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Example of overriding property access level using @Init. ```swift @Init(.accessLevel) ``` -------------------------------- ### Testing swift-syntax Versions Locally Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Command to test specific ranges of swift-syntax versions locally. ```bash SWIFT_SYNTAX_VERSION="600.0.0..<601.0.0" swift test ``` -------------------------------- ### Testing with Private State Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example demonstrating how @MemberwiseInit can facilitate testing by allowing injection of private state. ```swift @MemberwiseInit class ViewModel { let id: String private var _state: State // Tests can now inject private state: // ViewModel(id: "test", _state: .mock) } ``` -------------------------------- ### Complex Property Wrapper Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md An example showcasing complex property wrapper initialization with `@InitWrapper` and `@State`. ```swift ```swift @MemberwiseInit struct BindingExample: View { @InitWrapper(type: Binding.self) @Binding var text: String @InitWrapper(type: Binding.self, default: 0) @State var count: Int } // init(text: Binding, count: Binding = Binding(get: { 0 }, set: { _ in })) ``` ``` -------------------------------- ### Troubleshooting Stale Build Artifacts Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Commands to clean stale build artifacts. ```bash make clean rm -rf .build-*-* ``` -------------------------------- ### Minimal Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Generates a public memberwise initializer for a public struct with public properties. ```swift import MemberwiseInit @MemberwiseInit(.public) public struct Person { public let name: String public let age: Int } // Generates: // public init(name: String, age: Int) { // self.name = name // self.age = age // } ``` -------------------------------- ### Access Control Safety Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Example of an error when a property's access level is more restrictive than the target access level. ```swift @MemberwiseInit(.public) public struct Person { public let name: String private var age: Int? // 🛑 Error: would leak private property } ``` -------------------------------- ### Attachment Error Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example demonstrating the error when @MemberwiseInit is attached to an enum. ```swift @MemberwiseInit enum Status { case active } // Error: @MemberwiseInit can only be attached to a struct, class, or actor; // not to an enum. ``` -------------------------------- ### With SwiftUI State Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md An example of using the @MemberwiseInit macro with SwiftUI's @State property wrapper. ```swift import SwiftUI import MemberwiseInit @MemberwiseInit struct CounterView: View { @Init @State var count: Int = 0 var body: some View { Text("\(count)") } } // Expands to: // internal init(count: Int = 0) { // self.count = count // } ``` -------------------------------- ### Internal Memberwise Initializer Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example of using @MemberwiseInit without arguments to provide an internal memberwise initializer. ```swift @MemberwiseInit ``` -------------------------------- ### Type Inference Examples Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Demonstrates how the MemberwiseInit macro infers types from property initializers when explicit type annotations are omitted. ```swift @MemberwiseInit struct Config { var count = 0 // Inferred as Int var enabled = true // Inferred as Bool var items = [String]() // Inferred as [String] var mapping = ["a": 1] // Inferred as [String: Int] } ``` -------------------------------- ### Internal Utility Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit for an internal helper struct. ```swift @MemberwiseInit struct InternalHelper { let id: String var cache: [String: Any] = [: ] } ``` -------------------------------- ### Generic Containers Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit with a generic container struct. ```swift @MemberwiseInit struct Box { let value: T let metadata: [String: Any] = [: ] } ``` -------------------------------- ### Reserved Names and Keywords Validation Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/types.md Shows examples of invalid and valid custom parameter labels to avoid conflicts with Swift keywords and existing parameters. ```swift // Invalid: @Init(label: "init") // Swift keyword @Init(label: "self") // Reserved identifier @Init(label: "Type") // Existing parameter name conflict // Valid: @Init(label: "with") // Custom label @Init(label: "_") // Wildcard (label-less) ``` -------------------------------- ### Example Usage of @Init Attributes Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Demonstrates how to use @Init attributes to control property inclusion and access levels in generated initializers. ```swift @MemberwiseInit(.public) public struct User { public let id: String @Init(.public) private var email: String @Init(.ignore) public var derivedValue: String = computed() } // Expands to: // public init(id: String, email: String) { // self.id = id // self.email = email // // derivedValue is not included // } ``` -------------------------------- ### Static/Lazy Property Warning Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example showing the warning when an @Init attribute is applied to a static property. ```swift @MemberwiseInit struct Config { @Init static let version = "1.0" // Warning: @Init can't be applied to 'static' members } ``` -------------------------------- ### Deprecated EscapingConfig Usage Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Shows an example of using the deprecated '@Init(.escaping)' form and the resulting warning. ```swift @MemberwiseInit struct Handler { @Init(.escaping) let callback: Callback // Warning: EscapingConfig is deprecated } ``` -------------------------------- ### Default Values for `let` properties Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of providing a default value for a `let` property using @Init. ```swift @Init(default: "light") let theme: String ``` -------------------------------- ### Troubleshooting OOM errors Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Command to reconfigure Podman for OOM errors. ```bash bin/setup-podman # Choose "Light" or reconfigure with less memory ``` -------------------------------- ### SPM Package Dependency Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example of how to add MemberwiseInit as a package dependency in an SPM-based project. ```swift dependencies: [ .package(url: "https://github.com/gohanlon/swift-memberwise-init-macro", from: "0.6.0") ] ``` ```swift .product(name: "MemberwiseInit", package: "swift-memberwise-init-macro"), ``` -------------------------------- ### Linux Testing with Podman Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Command to run the full test matrix on Linux using Podman with auto-detected parallelism. ```bash make test-linux # Run full matrix with auto-detected parallelism ``` -------------------------------- ### Default Values for `var` properties Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of providing a default value for a `var` property. ```swift var theme = "light" // Type inferred, default applied ``` -------------------------------- ### CounterView with @InitWrapper for @Binding Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example of using @InitWrapper with MemberwiseInit for a property wrapped by @Binding. ```swift import SwiftUI @MemberwiseInit struct CounterView: View { @InitWrapper(type: Binding.self) @Binding var count: Int var body: some View { … } } ``` -------------------------------- ### Multiple Configurations Resolution Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example of how to resolve multiple configurations by using a single attribute with combined parameters. ```swift @Init(.public, default: "default", label: "value") let value: String ``` -------------------------------- ### All Properties Included Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/unchecked-memberwise-init.md Demonstrates how @_UncheckedMemberwiseInit includes all stored properties regardless of access level. ```swift @_UncheckedMemberwiseInit(.public) public struct APIResponse { public let id: String @Monitored internal var statusCode: Int private var rawResponse: Data } // Expands to: // public init( // id: String, // statusCode: Int, // rawResponse: Data // ) { // self.id = id // self.statusCode = statusCode // self.rawResponse = rawResponse // } ``` -------------------------------- ### Access Levels Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Examples of controlling initializer visibility and safety using different access levels with @MemberwiseInit. ```swift @MemberwiseInit(.public) // Public initializer @MemberwiseInit(.internal) // Module-scoped (default) @MemberwiseInit(.private) // File-scoped @MemberwiseInit(.package) // Package-scoped (Swift 5.9+) ``` -------------------------------- ### Public Library Type Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit with public access level for a library type. ```swift @MemberwiseInit(.public) public struct LibraryType { public let required: String @Init(default: []) public let optional: [String] } ``` -------------------------------- ### Access Level Control Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/unchecked-memberwise-init.md Illustrates how per-property access level control via @Init still applies. ```swift @_UncheckedMemberwiseInit(.public) public struct Example { public let publicProp: String @Init(.internal) private let restrictedProp: String } // The public init parameter for restrictedProp uses the internal access specified by @Init ``` -------------------------------- ### Basic Struct with Public Initializer Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md An example of a basic struct with a public memberwise initializer generated by the @MemberwiseInit macro. ```swift import MemberwiseInit @MemberwiseInit(.public) public struct Point { public let x: Double public let y: Double } // Usage: let origin = Point(x: 0, y: 0) ``` -------------------------------- ### Example Usage of MemberwiseInit with Type Inference Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Demonstrates how MemberwiseInit infers types from various expressions, including literals, ranges, arrays, dictionaries, tuples, and bitwise operations. ```swift @MemberwiseInit public struct Example { var string = "", int = 0 var boolTrue = true var mixedDivide = 8.0 / 4 // Double var halfOpenRange = 1.0..<5 // Range var arrayTypeInit = [T]() var arrayIntLiteral = [1, 2, 3] var arrayPromoted = [1, 2.0] // [Double] var nestedArray = [[1, 2], [20, 30]] // [[Int]] var dictionaryTypeInit = [String: T]() var dictionaryLiteral = ["key1": 1, "key2": 2] var dictionaryPromoted = [1: 2.0, 3.0: 4] // [Double: Double] var nestedDictionary = ["key1": ["subkey1": 10], "key2": ["subkey2": 20]] // [String: [String: Int]] var tuple = (1, ("Hello", true)) var value = T.allCases.first as T? var nestedMixed = ((1 + 2) * 3) >= (4 / 2) && ((true || false) && !(false)) // Bool var bitwiseAnd = 0b1010 & 0b0101 var leftShift = 1 << 2 var bitwiseNotInt = ~0b0011 var intBinary = 0b01010101 var intOctal = 0o21 var intHex = 0x1A var floatExponential = 1.25e2 // Double var floatHex = 0xC.3p0 // Double var arrayAs = [1, "foo", 3] as [Any] var dictionaryAs = ["foo": 1, 3: "bar"] as [AnyHashable: Any] } ``` -------------------------------- ### @Init on Already-Initialized Let Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Presents a warning when '@Init' (without parameters) is applied to a 'let' property that already has an initializer. ```swift @MemberwiseInit struct Config { @Init let version: String = "1.0" // Warning: @Init can't be applied to already initialized constant } ``` -------------------------------- ### Exposing Private Properties Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Demonstrates how to expose private properties in the generated initializer using `@Init(.public)`. ```swift ```swift @MemberwiseInit(.public) public struct User { public let id: String @Init(.public) private let email: String @Init(.public) private var verified: Bool = false } // Generated init: // public init(id: String, email: String, verified: Bool = false) ``` ``` -------------------------------- ### With Custom Labels Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Illustrates using custom parameter labels for properties in the generated initializer. ```swift @MemberwiseInit struct Point { @Init(label: "_") let x: Int @Init(label: "_") let y: Int } // Generates: // internal init(_ x: Int, _ y: Int) { ... } ``` -------------------------------- ### Type Compatibility Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/types.md Demonstrates valid and invalid type assignments for properties when using the @Init attribute, highlighting type compatibility requirements. ```swift // Valid: let value: Int = 0 // Initializer parameter type: Int @Init(type: Int.self) let value: Int = 0 // Override to Int // Invalid: @Init(type: String.self) let value: Int = 0 // Type mismatch: String vs Int ``` -------------------------------- ### Variable Initializer vs @Init(default:) Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Shows the preferred way to initialize `var` and `let` properties using variable initializers or `@Init(default:)`. ```swift // Recommended for var: var count = 0 // Less idiomatic: @Init(default: 0) var count: Int // Required for let (can't reinitialize immutable properties): @Init(default: 0) let count: Int // Invalid syntax: let count: Int = 0 // Initializer takes precedence; @Init(default:) ignored ``` -------------------------------- ### String-Based Type References Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/types.md Demonstrates how to use string-based type references with @InitRaw for dynamic type specifications. ```swift @InitRaw(type: Binding.self) var count: Int @InitRaw(type: [String: Any].self) var data: NSDictionary ``` -------------------------------- ### Troubleshooting Persistent Compiler Crashes Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/DEVELOPMENT.md Command to debug specific Swift and swift-syntax version combinations for persistent compiler crashes. ```bash ./bin/test-linux --swift --swift-syntax --verbose ``` -------------------------------- ### SwiftUI View without MemberwiseInit Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md An example of a SwiftUI View struct without the MemberwiseInit macro applied, demonstrating the default internal memberwise initializer. ```swift import SwiftUI struct MyView: View { @State var isOn: Bool var body: some View { … } } ``` -------------------------------- ### Invalid Label Error Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example demonstrating the error when an invalid custom parameter label is used. ```swift @MemberwiseInit struct Point { @Init(label: "123invalid") let x: Int // Error: Invalid label value } ``` -------------------------------- ### With Default Values Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Shows how to provide default values for properties in the generated initializer. ```swift @MemberwiseInit struct Configuration { var theme: String = "light" @Init(default: 30) let timeout: Int var retries: Int = 3 } // Generates: // internal init( // theme: String = "light", // timeout: Int = 30, // retries: Int = 3 // ) { ... } ``` -------------------------------- ### Standard Property Wrapper Configuration Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Demonstrates how to configure initializers for properties using standard property wrappers with `@InitWrapper`. ```swift @MemberwiseInit struct ToggleView: View { @InitWrapper(type: Binding.self) @Binding var isEnabled: Bool } // init(isEnabled: Binding) ``` -------------------------------- ### Parameter Label Formats Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Illustrates different ways to configure parameter labels for the generated initializer. ```swift // Implicit label (derived from property name): let count: Int // → init(count: Int) // Custom label: @Init(label: "numberOfItems") let count: Int // → init(numberOfItems count: Int) // Label-less (wildcard): @Init(label: "_") let count: Int // → init(_ count: Int) // Empty label (invalid): @Init(label: "") let count: Int // → Error: Invalid label value ``` -------------------------------- ### Custom Label on Multiple Bindings Error Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example showing the error when a custom label is applied to a property with multiple bindings. ```swift @MemberwiseInit struct Point { @Init(label: "coords") let x = 0, y = 0 // Error: Custom 'label' can't be applied to multiple bindings } ``` -------------------------------- ### Access Level Leak Error Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example demonstrating the error when a property's access level is more restrictive than the initializer's. ```swift @MemberwiseInit(.public) public struct Person { public let name: String private var age: Int? // 🛑 Error } // Error: @MemberwiseInit(.public) would leak access to 'private' property ``` -------------------------------- ### Codable Type with Internal Fields Usage Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/unchecked-memberwise-init.md Provides a complete usage example of a Codable struct with internal fields initialized using @_UncheckedMemberwiseInit. ```swift import Foundation @_UncheckedMemberwiseInit struct User: Codable { let id: Int let name: String let email: String internal var _isCached: Bool = false private var _cachedAt: Date? enum CodingKeys: String, CodingKey { case id, name, email } } // Usage: let user = User(id: 1, name: "Alice", email: "alice@example.com", _isCached: true, _cachedAt: nil) ``` -------------------------------- ### Using @MemberwiseInit Macro Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Applying the @MemberwiseInit macro to automatically generate a memberwise initializer. ```swift @MemberwiseInit // ", public struct Person { public let name: String } ``` -------------------------------- ### Deprecated _deunderscoreParameters Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Demonstrates the usage of the deprecated '_deunderscoreParameters' parameter and its warning. ```swift @MemberwiseInit(.public, _deunderscoreParameters: true) public struct Review { @Init(.public) private let _rating: Int } // Warning: '_deunderscoreParameters' is deprecated ``` -------------------------------- ### Default Behavior (No Parameters) Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md When applied without parameters, generates an initializer with internal access. ```swift @MemberwiseInit struct Person { public let name: String public let age: Int } // Expands to: // internal init(name: String, age: Int) { // self.name = name // self.age = age // } ``` -------------------------------- ### Tuple Destructuring Not Supported Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example demonstrating that tuple destructuring in property declarations is not supported by `@MemberwiseInit`. ```swift @MemberwiseInit struct Point2D { let (x, y): (Int, Int) //┬───────────────────── //╰─ 🛑 @MemberwiseInit does not support tuple destructuring for // property declarations. Use multiple declartions instead. } ``` -------------------------------- ### InitWrapper vs InitRaw Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Compares `@InitWrapper` and `@InitRaw` for configuring property wrappers, highlighting `@InitWrapper`'s conciseness. ```swift // InitWrapper (convenient): @InitWrapper(type: Binding.self) @Binding var flag: Bool // InitRaw (explicit): @InitRaw(assignee: "self._flag", type: Binding.self) @Binding var flag: Bool // Both generate identical code. @InitWrapper is more concise for standard property wrappers. ``` -------------------------------- ### Invalid Label Resolution Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example of using valid Swift identifiers or '_' for parameter labels. ```swift @Init(label: "horizontal") let x: Int @Init(label: "_") let y: Int // Label-less ``` -------------------------------- ### File Structure Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/INDEX.md Overview of the project's file structure, indicating the location of key documentation files. ```swift output/ ├── README.md # START HERE ├── INDEX.md # This file ├── OVERVIEW.txt # Summary of contents ├── configuration.md # Parameter reference ├── errors.md # Error catalog ├── types.md # Type definitions └── api-reference/ ├── memberwise-init-macro.md # @MemberwiseInit ├── init-attributes.md # @Init, @InitWrapper, @InitRaw └── unchecked-memberwise-init.md # @_UncheckedMemberwiseInit ``` -------------------------------- ### Multiple Parameters on Single Attribute Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Demonstrates how to use multiple parameters like access level, label, default value, and escaping for an init attribute. ```swift @MemberwiseInit struct Complex { @Init(.public, label: "count", default: 0, escaping: true) private let handler: Int = 0 } ``` -------------------------------- ### Infer Type from Property Initialization Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example where Swift infers the type from the property's initialization expression. ```swift @MemberwiseInit struct Example { var count = 0 // 👈 `Int` is inferred } ``` -------------------------------- ### Attributed Properties Included Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/unchecked-memberwise-init.md Shows that properties with attributes are included by default. ```swift import SwiftUI @_UncheckedMemberwiseInit(.public) public struct MyView: View { @State var count: Int public var body: some View { /* ... */ } } // Expands to: // public init(count: Int) { // self.count = count // } // Warning: @State properties typically shouldn't be initialized this way ``` -------------------------------- ### Basic Form (Internal Access) Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Generates an init method with internal access level by default. ```swift @attached(member, names: named(init)) public macro MemberwiseInit( _deunderscoreParameters: Bool? = nil, _optionalsDefaultNil: Bool? = nil ) = #externalMacro( module: "MemberwiseInitMacros", type: "MemberwiseInitMacro" ) ``` -------------------------------- ### Access Level Form Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Generates an init method at the specified access level with optional customization. ```swift @attached(member, names: named(init)) public macro MemberwiseInit( _ accessLevel: AccessLevelConfig, _deunderscoreParameters: Bool? = nil, _optionalsDefaultNil: Bool? = nil ) = #externalMacro( module: "MemberwiseInitMacros", type: "MemberwiseInitMacro" ) ``` -------------------------------- ### With Mixed Access Levels Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Demonstrates generating a public initializer while handling properties with different access levels and default values. ```swift @MemberwiseInit(.public) public struct User { public let id: String @Init(.public) private var email: String? @Init(.ignore) private var verified: Bool = false } // Generates: // public init(id: String, email: String? = nil) { // self.id = id // self.email = email // } ``` -------------------------------- ### @InitRaw Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Low-level configuration with direct assignee control. ```swift @InitRaw(assignee: "self._backing", type: CustomType.self) let property: WrappedType ``` -------------------------------- ### Label-less Parameters using _ Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Shows how to use `@Init(label: "_")` to create label-less init parameters. ```swift ```swift @MemberwiseInit struct Point { @Init(label: "_") let x: Int @Init(label: "_") let y: Int } // init(_ x: Int, _ y: Int) ``` ``` -------------------------------- ### Custom Label on Multiple Bindings Resolution Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Example of resolving the multiple bindings error by separating declarations. ```swift @Init(label: "x") let x = 0 @Init(label: "y") let y = 0 ``` -------------------------------- ### Deprecated EscapingConfig Usage Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/types.md Example of deprecated usage for EscapingConfig. ```swift // Old (deprecated): @Init(.escaping, label: "handler") let callback: Callback // New (recommended): @Init(escaping: true, label: "handler") let callback: Callback ``` -------------------------------- ### Codable Types with Internal Fields Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/README.md Example of using @MemberwiseInit with Codable types that have internal fields. ```swift @MemberwiseInit(.public) public struct APIResponse: Codable { public let id: Int public let data: String internal let _createdAt: Date = Date() private let _hash: String = "" enum CodingKeys: String, CodingKey { case id, data } } ``` -------------------------------- ### Default Semantics for @Init() Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Demonstrates the special semantics of the `default` parameter for `nil` with `@Init()`. ```swift // No default value: @Init() let property: String? // Default to nil explicitly: @Init(default: nil) let property: String? // Default to a non-nil value: @Init(default: "default") let property: String? ``` -------------------------------- ### Missing Type Information Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Illustrates an error where a property lacks both a type annotation and an initializer with an inferrable type. ```swift @MemberwiseInit struct Example { let value // Error: No type annotation or initializer } ``` -------------------------------- ### @Init Macro - Main Signature (Configured Form) Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Configures a property's inclusion and behavior in the generated initializer. ```swift @attached(peer) public macro Init( _ accessLevel: AccessLevelConfig? = nil, default: Any? = nil, escaping: Bool? = nil, label: String? = nil ) = #externalMacro( module: "MemberwiseInitMacros", type: "InitMacro" ) ``` -------------------------------- ### Custom Parameter Labels Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Demonstrates using custom parameter labels for properties in the generated memberwise initializer, including the underscore label. ```swift @MemberwiseInit struct Rectangle { @Init(label: "_") let width: Int @Init(label: "_") let height: Int @Init(label: "fill") var fillColor: String = "white" } // Expands to: // internal init(_ width: Int, _ height: Int, fill fillColor: String = "white") { ... } ``` -------------------------------- ### De-underscoring Parameters Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example demonstrating how to use `_deunderscoreParameters: true` to omit underscores from initializer parameter names for properties prefixed with an underscore. ```swift @MemberwiseInit(.public, _deunderscoreParmeters: true) public struct Review { @Init(.public) private let _rating: Int public var rating: String { String(repeating: "⭐️", count: self._rating) } } ``` ```swift public init( rating: Int // 👈 Non-underscored parameter ) { self._rating = rating } ``` -------------------------------- ### Nil vs Explicit False for _optionalsDefaultNil Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Demonstrates the behavior of the _optionalsDefaultNil parameter. ```swift // No opinion (default behavior): @MemberwiseInit(_optionalsDefaultNil: nil) // nil is the same as omitting // Explicitly enabled: @MemberwiseInit(_optionalsDefaultNil: true) // Default all optionals to nil // Explicitly disabled: @MemberwiseInit(_optionalsDefaultNil: false) // Never default optionals to nil ``` -------------------------------- ### Custom Default on Initialized Var Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Shows an error when a custom default is applied to a 'var' property that already has an initializer. ```swift @MemberwiseInit struct Config { @Init(default: 100) var timeout: Int = 30 // Error: Custom 'default' can't be applied to already initialized variable } ``` -------------------------------- ### Custom Default on Initialized Let Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Demonstrates an error when a custom default is applied to a 'let' property that already has an initializer. ```swift @MemberwiseInit struct Config { @Init(default: 100) let timeout: Int = 30 // Error: @Init can't be applied to already initialized constant } ``` -------------------------------- ### Label-less Parameter Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Shows how to create label-less parameters for an initializer using the '_' label. ```swift @MemberwiseInit struct Point { @Init(label: "_") let x: Double @Init(label: "_") let y: Double } // Usage: let p = Point(1.0, 2.0) ``` -------------------------------- ### Custom Default on Multiple Bindings Example Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/errors.md Illustrates an error where a custom default value is applied to multiple bindings of a property. ```swift @MemberwiseInit struct Pair { @Init(default: 0) let x = 1, y = 2 // Error: Custom 'default' can't be applied to multiple bindings } ``` -------------------------------- ### Optional Properties with Experimental Flag Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Shows how to use the _optionalsDefaultNil flag with @MemberwiseInit to default optional properties to nil. ```swift @MemberwiseInit(.public, _optionalsDefaultNil: true) public struct APIResponse: Codable { public let id: String public let name: String? public let email: String? public let age: Int? } // Expands to: // public init( // id: String, // name: String? = nil, // email: String? = nil, // age: Int? = nil // ) { ... } ``` -------------------------------- ### Override with InitRaw Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Example of using `@InitRaw` to specify a different type for the initializer parameter than the property's declared type. ```swift @MemberwiseInit struct Container { @InitRaw(type: [String: Any].self) let data: NSDictionary } // init(data: [String: Any]) // Parameter type is [String: Any], not NSDictionary ``` -------------------------------- ### Selective Optional Defaulting Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Illustrates how to selectively apply a default `nil` value to specific optional properties using `@Init(default: nil)`. ```swift @MemberwiseInit(.public) public struct Config { public let required: String @Init(default: nil) public let optional: String? } // public init( // required: String, // optional: String? = nil // ) ``` -------------------------------- ### Memberwise Initializer with Specified Access Level Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Demonstrates using @MemberwiseInit with a specific access level like .public. ```swift @MemberwiseInit(.public) ``` -------------------------------- ### Property-Level Configuration Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Illustrates how property-level `@Init` configuration overrides macro-level settings. ```swift @MemberwiseInit(.public, _optionalsDefaultNil: true) struct Type { @Init(default: "") let x: String? // Override: uses custom default let y: Int? // Uses macro-level: nil default } ``` -------------------------------- ### Default Values for var properties Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Demonstrates using variable initializers for default values with `var` properties. ```swift ```swift @MemberwiseInit struct Config { var theme = "light" // Default applied automatically } ``` ``` -------------------------------- ### Generated Memberwise Initializer Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md This example shows how the MemberwiseInit macro ignores a private property (`age`) and generates a public initializer that only includes the public properties (`name`). ```swift public init( // ": `public`, ignoring `age` property name: String ) { self.name = name } ``` -------------------------------- ### Sendable Closures Configuration Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/configuration.md Illustrates how to configure initializers for properties using `@Sendable` closures, ensuring proper escaping. ```swift typealias SendableHandler = @Sendable () -> Void @MemberwiseInit struct SafeTask { @Init(escaping: true) let handler: SendableHandler } // init(handler: @escaping SendableHandler) ``` -------------------------------- ### Default Values for let properties using @Init(default:) Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/init-attributes.md Shows how to use `@Init(default:)` for default values with `let` properties, as they cannot be reinitialized. ```swift ```swift @MemberwiseInit struct Config { @Init(default: Color.blue) let backgroundColor: Color } ``` ``` -------------------------------- ### Overriding De-underscore Behavior Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example showing how to use `@Init(label: String)` to explicitly retain the underscore for an initializer parameter, overriding the global de-underscore setting. ```swift @MemberwiseInit(.public, _deunderscoreParameters: true) public struct Review { @Init(.public, label: "_rating") private let _rating: Int } ``` ```swift public init( _rating: Int // 👈 Underscored parameter ) { self._rating = _rating } ``` -------------------------------- ### Access Level Control Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/_autodocs/api-reference/memberwise-init-macro.md Specifying an access level makes the generated initializer accessible at that level. ```swift @MemberwiseInit(.public) public struct Person { public let name: String public let age: Int } // Expands to: // public init(name: String, age: Int) { // self.name = name // self.age = age // } ``` -------------------------------- ### Defaulting Optionals to Nil (var property) Source: https://github.com/gohanlon/swift-memberwise-init-macro/blob/main/README.md Example demonstrating how a `var` property initialized to `nil` naturally leads to a default `nil` parameter value in the generated initializer. ```swift @MemberwiseInit(.public) public struct User { public var name: String? = nil // 👈 } ``` ```swift _ = User() // 'name' defaults to 'nil' ``` ```swift public init( name: String? = nil // 👈 ) { self.name = name } ```