vds_ios/VDS/Protocols/AnyEquatable.swift
Matt Bruce 48797d2003 updated to have a casted isEqual
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-15 16:58:54 -05:00

62 lines
1.4 KiB
Swift

//
// AnyEquatable.swift
// VDS
//
// Created by Matt Bruce on 9/15/22.
//
import Foundation
public protocol AnyEquatable {
func isEqual(_ equatable: Self) -> Bool
}
extension AnyEquatable {
public func isEqual(_ anyEquatable: any AnyEquatable) -> Bool {
guard let equatable = anyEquatable as? Self else {
return false
}
return isEqual(equatable)
}
}
func == (lhs: (any AnyEquatable)?, rhs: (any AnyEquatable)?) -> Bool {
switch (lhs, rhs) {
case (.some(let lhs), .some(let rhs)):
return lhs.isEqual(rhs)
case (.none, .none):
return true
default:
return false
}
}
func != (lhs: (any AnyEquatable)?, rhs: (any AnyEquatable)?) -> Bool {
return !(lhs == rhs)
}
func == (lhs: [any AnyEquatable]?, rhs: [any AnyEquatable]?) -> Bool {
switch (lhs, rhs) {
case (.some(let lhs), .some(let rhs)):
return lhs == rhs
case (.none, .none):
return true
default:
return false
}
}
func != (lhs: [any AnyEquatable]?, rhs: [any AnyEquatable]?) -> Bool {
return !(lhs == rhs)
}
func == (lhs: [any AnyEquatable], rhs: [any AnyEquatable]) -> Bool {
return lhs.elementsEqual(rhs, by: { (lhsElement, rhsElement) -> Bool in
return lhsElement == rhsElement
})
}
func != (lhs: [any AnyEquatable], rhs: [any AnyEquatable]) -> Bool {
return !(lhs == rhs)
}