Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DavidBemerguy/f7f61acce2f9b5d8be0b18aa13e55d84 to your computer and use it in GitHub Desktop.
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
@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