i have an error type that wraps another existential any Error:
@frozen public
struct RecursiveError<Location>:Error
{
/// The location (key or index) where the error occurred.
public
let location:Location
/// The underlying error that occurred.
public
let error:any Error
@inlinable public
init(_ error:any Error, in location:Location)
{
self.location = location
self.error = error
}
}
now i want to test if my API is throwing the correct error with a unit test like:
$0.test(name: "int32-to-uint8", decoding: bson,
failure: RecursiveError<String>.init(
IntegerOverflowError<UInt8>.int32(.max),
in: "int32"))
{
try $0["int32"].decode(to: UInt8.self)
}
but that would require RecursiveError to be Equatable. and making RecursiveError Equatable requires comparing two any Error existentials. is there a way to do this?
update: got this to work
extension Error where Self:Equatable
{
fileprivate
func equals(_ other:any Error) -> Bool
{
(other as? Self).map { $0 == self } ?? false
}
}
tera
3
I didn't try it but it should work:
extension Error where Self: Equatable {
func isEqualTo(other: Error) -> Bool {
if let o = other as? Self { return self == o }
return false
}
}
let a: any Error & Equatable = ...
let b: any Error & Equatable = ...
let equal = a.isEqual(b) && b.isEqual(a)
Inspired by the "bridge building" part of the famous Crusty video.
PS. corrected the code above.