Codable synthesis for enums with associated values

Happy to see a pitch in this area! I think this is a good start in the direction of having something in place for enums which has been missing for a long time. I'm wondering about a few cases here which are not explicitly discussed in the pitch:

  1. What about enum cases without associated values? Are those still encoded as keyed containers, or single values? For instance, .load in

    enum Command: Codable {
        case load
        case store(key: String, value: Int)
    }
    
  2. What about enum cases that share a name? For instance, both cases of

    enum Foo {
        case bar(String, id: Int)
        case bar(String, value: Double)
    }
    

    could end up with the same encoded representation ({"bar": ["abc", 123]} could match either). Would the synthesized initializer try cases in order, or how would that work?

  3. It would be nice to see some Alternatives Considered/Future Directions with some discussion about other approaches. For instance, from a lot of discussion in Automatic Codable conformance for enums with associated values that themselves conform to Codable and similar threads, we know that different folks have differing opinions on what they would expect to be a default implementation based on their needs.

6 Likes