Flight-School / AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values

Home Page:https://flight.school/books/codable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AnyEncodable conformance to Equatable

stefanprutianu opened this issue · comments

Hi @mattt,

I was exploring your project and I have a short question.

Am I assuming incorrectly that the assertion bellow should hold?

let t0: AnyEncodable = ["x": 1]
let t1: AnyEncodable = ["x": 1]

assert(t0 == t1, "These should be equal")

I've read the conformance of AnyEncodable to Equatable and it's not clear to me if my expectations are wrong or there's something missing.

Thanks,
Stefan

commented

Apologies for the late response, @stefanprutianu.

Equatability holds for AnyEncodable, but things can get ambiguous when literals are involved. An == test works if you explicitly cast 1 as AnyEncodable or recast each declaration as [String: AnyEncodable]

let t0: AnyEncodable = ["x": 1 as AnyEncodable]
let t1: AnyEncodable = ["x": 1 as AnyEncodable]
t0 == t1 // true

let u0: [String: AnyEncodable] = ["x": 1]
let u1: [String: AnyEncodable] = ["x": 1]
u0 == u1 // true

Based on this, I think the correct resolution for this issue would be to remove ExpressibleByDictionaryLiteral conformance from AnyCodable types (you would still be able to declare as [String: AnyEncodable] or [AnyHashable: AnyEncodable]).
Edit: Maybe not. That would break nested dictionary literals. I'll have to think about other approaches.

This issue has solved by #51.