Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Observation and simplify isEqual #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Simplify isEqual implementation
hashemi committed Aug 12, 2024
commit 54e856f6d7a149cc5432ce51a3363d98481afb16
26 changes: 5 additions & 21 deletions Sources/NotSwiftUIState/AnyEquatable.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
// Check if the two values are Equatable and equal
func isEqual(_ lhs: Any, _ rhs: Any) -> Bool {
func f<LHS>(lhs: LHS) -> Bool {
if let typeInfo = Wrapped<LHS>.self as? AnyEquatable.Type {
return typeInfo.isEqual(lhs: lhs, rhs: rhs)
}
return false
}
return _openExistential(lhs, do: f)
}

protocol AnyEquatable {
static func isEqual(lhs: Any, rhs: Any) -> Bool
}

enum Wrapped<T> { }

extension Wrapped: AnyEquatable where T: Equatable {
static func isEqual(lhs: Any, rhs: Any) -> Bool {
guard let l = lhs as? T, let r = rhs as? T else {
return false
}
return l == r
guard let lhs = lhs as? any Equatable else { return false }
func f<LHS: Equatable>(_ lhs: LHS) -> Bool {
guard let rhs = rhs as? LHS else { return false }
return lhs == rhs
}
return f(lhs)
}