SE-0295: Codable synthesis for enums with associated values

I like most of the details of this proposal quite well. Thank you for working on it!

I was thinking the same when I read the proposal. +1 to this idea.

+1 to this idea as well. The _caseName convention does not feel right for Swift.

The other point of feedback I have is that I do not think unkeyed containers should be used for associated values. Instead, I think we should establish a convention for handling them and recommend use of the coding keys to override the default. The first idea that comes to mind for the convention is to use implicit positional names such as _0, _1, etc for unlabeled associated values.

By default, this enum:

enum Command: Codable {
  case load(String)
  case store(String, value: Int)
}

would encode a store value as:

{
    “store”: {
        “_0”: “some string”,
        “value”: 42
    }
}

To override this, you would declare:

enum CodingKeys: CodingKey {
    case load
    case store

    enum Store: String, CodingKey {
        case _0 = “keyForEncoding”
        case value
    }
}
9 Likes