Uploaded image for project: 'Swift'
  1. Swift
  2. SR-9545

Decodable gotcha: "let" with default + coding key

    XMLWordPrintable

    Details

      Description

      Structs with default let properties that are codable will always prefer the default.

      When an explicit coding keys is provided, Swift probably should not compile:

      struct Foo: Decodable {
        let bar = true
      
        private enum CodingKeys: String, CodingKey {
          case bar
        }
      }
      

      The bar coding key should be a red flag since that key will never be decoded in a synthesized decodable initializer.

      try JSONDecoder().decode(Foo.self, from: Data("""
      {"bar" : false}
      """.utf8).bar == false
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              stephencelis Stephen Celis
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: