|
|
|
|
|
by itaiferber
1030 days ago
|
|
Codable (along with other derived conformances like Equatable, Hashable, and RawRepresentable) is indeed built in to the compiler[0], but unlike Serde, it operates during type-checking on a fully-constructed AST (with access to type information), manipulating the AST to insert code. Because it operates at a later stage of compilation and at a much higher level (with access to type information), the work necessary is significantly less. (It's difficult to compare Serde and Codable in a way that isn't apples-and-oranges, but at the very least, Codable makes use of information that the compiler has done, and has already needed to do anyway; there's very little manipulation it needs to do to achieve its aims.) With ongoing work for Swift macros, it may eventually be possible to rip this code out of the compiler and rewrite it as a macro, though it would need to be a semantic macro[1] rather a syntactic one, which isn't currently possible in Swift[2]. [0] https://github.com/apple/swift/blob/main/lib/Sema/DerivedCon...
[1] https://gist.github.com/DougGregor/4f3ba5f4eadac474ae62eae83...
[2] https://forums.swift.org/t/why-arent-macros-given-type-infor... |
|