Created
May 4, 2023 14:51
-
-
Save DavidBemerguy/f7f61acce2f9b5d8be0b18aa13e55d84 to your computer and use it in GitHub Desktop.
PropertyWrapper that forces the compiler to ignore non hashable or non equatable properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@propertyWrapper | |
public struct IgnoreEquatable<Wrapped>: Equatable { | |
public var wrappedValue: Wrapped | |
public static func == ( | |
lhs: IgnoreEquatable<Wrapped>, | |
rhs: IgnoreEquatable<Wrapped> | |
) -> Bool { | |
true | |
} | |
public init(wrappedValue: Wrapped) { | |
self.wrappedValue = wrappedValue | |
} | |
} | |
@propertyWrapper | |
public struct IgnoreHashable<Wrapped>: Hashable { | |
@IgnoreEquatable public var wrappedValue: Wrapped | |
public func hash(into hasher: inout Hasher) {} | |
public init(wrappedValue: Wrapped) { | |
self.wrappedValue = wrappedValue | |
} | |
} | |
// Example | |
struct ViewModel: Equatable { | |
let id: String | |
let name: String | |
@IgnoreEquatable var action: () -> Void // Note the change from `var` to `let` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment