|
67 | 67 | /// } |
68 | 68 | /// } |
69 | 69 | /// ``` |
| 70 | +/// |
| 71 | +/// |
| 72 | +/// ## Isolation |
| 73 | +/// `Equatable` macro supports generating the conformance with different isolation levels by using the `isolation` parameter. |
| 74 | +/// The parameter accepts three values: `.nonisolated` (default), `.isolated`, and `.main` (requires Swift 6.2 or later). |
| 75 | +/// The chosen isolation level will be applied to the generated conformances for both `Equatable` and `Hashable` (if applicable). |
| 76 | +/// |
| 77 | +/// ### Nonisolated (default) |
| 78 | +/// The generated `Equatable` conformance is `nonisolated`, meaning it can be called from any context without isolation guarantees. |
| 79 | +/// ```swift |
| 80 | +/// @Equatable(isolation: .nonisolated) (also ommiting the parameter uses this mode) |
| 81 | +/// struct Person { |
| 82 | +/// let name: String |
| 83 | +/// let age: Int |
| 84 | +/// } |
| 85 | +/// ``` |
| 86 | +/// |
| 87 | +/// expands to: |
| 88 | +/// ```swift |
| 89 | +/// extension Person: Equatable { |
| 90 | +/// nonisolated public static func == (lhs: Person, rhs: Person) -> Bool { |
| 91 | +/// lhs.name == rhs.name && lhs.age == rhs.age |
| 92 | +/// } |
| 93 | +/// } |
| 94 | +/// ``` |
| 95 | +/// |
| 96 | +/// ### Isolated |
| 97 | +/// The generated `Equatable` conformance is `isolated`, meaning it can only be called from within the actor's context. |
| 98 | +/// ```swift |
| 99 | +/// @Equatable(isolation: .isolated) |
| 100 | +/// struct Person { |
| 101 | +/// let name: String |
| 102 | +/// let age: Int |
| 103 | +/// } |
| 104 | +/// ``` |
| 105 | +/// |
| 106 | +/// expands to: |
| 107 | +/// ```swift |
| 108 | +/// extension Person: Equatable { |
| 109 | +/// public static func == (lhs: Person, rhs: Person) -> Bool { |
| 110 | +/// lhs.name == rhs.name && lhs.age == rhs.age |
| 111 | +/// } |
| 112 | +/// } |
| 113 | +/// ``` |
| 114 | +/// |
| 115 | +/// ### Main (requires Swift 6.2 or later) |
| 116 | +/// A common case is to have a `@MainActor` isolated type, SwiftUI views being a common example. Previously, the generated `Equatable` conformance had to be `nonisolated` in order to satisfy the protocol requirement. |
| 117 | +/// This would then restrict us to access only nonisolated properties of the type in the generated `Equatable` function — which ment that we had to ignore all `@MainActor` isolated properties in the equality comparison. |
| 118 | +/// Swift 6.2 introduced [isolated confomances](https://docs.swift.org/compiler/documentation/diagnostics/isolated-conformances/) allowing us to generate `Equatable` confomances |
| 119 | +/// which are bound to the `@MainActor`. In this way the generated `Equatable` conformance can access `@MainActor` isolated properties of the type synchonously and the compiler will guarantee that the confomance |
| 120 | +/// will be called only from the `@MainActor` context. |
| 121 | +/// |
| 122 | +/// We can do so by specifying `@Equatable(isolation: .main)`, e.g: |
| 123 | +/// ```swift |
| 124 | +/// @Equatable(isolation: .main) |
| 125 | +/// @MainActor |
| 126 | +/// struct Person { |
| 127 | +/// let name: String |
| 128 | +/// let age: Int |
| 129 | +/// } |
| 130 | +/// ``` |
| 131 | +/// |
| 132 | +/// expands to: |
| 133 | +/// ```swift |
| 134 | +/// extension Person: Equatable { |
| 135 | +/// public static func == (lhs: Person, rhs: Person) -> Bool { |
| 136 | +/// lhs.name == rhs.name && lhs.age == rhs.age |
| 137 | +/// } |
| 138 | +/// } |
| 139 | +/// ``` |
| 140 | +/// |
70 | 141 | @attached(extension, conformances: Equatable, Hashable, names: named(==), named(hash(into:))) |
71 | | -public macro Equatable() = #externalMacro(module: "EquatableMacros", type: "EquatableMacro") |
| 142 | +public macro Equatable(isolation: Isolation = .nonisolated) = #externalMacro(module: "EquatableMacros", type: "EquatableMacro") |
| 143 | + |
| 144 | +/// Isolation level for the generated Equatable functions. |
| 145 | +public enum Isolation { |
| 146 | + /// The generated `Equatable` conformance is `nonisolated`. |
| 147 | + case nonisolated |
| 148 | + /// The generated `Equatable` conformance is`isolated`. |
| 149 | + case isolated |
| 150 | + #if swift(>=6.2) |
| 151 | + /// The generated `Equatable` conformance is `@MainActor` isolated. |
| 152 | + case main |
| 153 | + #endif |
| 154 | +} |
72 | 155 |
|
73 | 156 | /// A peer macro that marks properties to be ignored in `Equatable` conformance generation. |
74 | 157 | /// |
|
0 commit comments