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

RawRepresentable decodable encodable inits not work on release

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Environment:

      Xcode 9.3, swift 4.1

      Description

      JSON string

      {
          "id": 5
          "name": "Name"
      }
      

      Must be struct with type safe id

      struct User: Hashable, Codable {
          let id: GenericID<User>
          let name: String
      }
      

      Codable actions not fire errors for below code on debug mode. But on release mode there are codable errors

      struct GenericID<T>: RawRepresentable, Hashable, Codable {
          let rawValue: Int
          
          init(rawValue: Int) { self.rawValue = rawValue }
      }
      

      Adding codable custom inits  fix errors

      struct GenericID<T>: RawRepresentable, Hashable, Codable {
          let rawValue: Int
          
          init(rawValue: Int) { self.rawValue = rawValue }
          
          init(from decoder: Decoder) throws {
              let container = try decoder.singleValueContainer()
              rawValue = try container.decode(Int.self)
          }
          
          func encode(to encoder: Encoder) throws {
              var container = encoder.singleValueContainer()
              try container.encode(rawValue)
          }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                aznix Elshad Yarmetov
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated: