Flight-School / AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values
https://flight.school/books/codable
MIT License
1.29k stars 133 forks source link

Question About Use of Generic in Initializer #2

Closed pteasima closed 6 years ago

pteasima commented 6 years ago

Hi, love the work. I wonder why its not init<T: Codable>(_ value: T?). Could this catch more errors at compile time?

mattt commented 6 years ago

The pattern for type-erased containers, such as AnyHashable, is for the designated initializer to take a type conforming to the relevant protocol. However, in the case of AnyCodable, I was unable to make this work. I left the initializer here to take a generic parameter as a way to acknowledge this pattern; it could just as well take Any? instead.

If anyone can suggest a way to do this correctly, please submit a PR!