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

Conform Range to Codable when Bound conforms to Codable

    XMLWordPrintable

    Details

      Description

      It is possible to conform Range to Codable when Bound is Codable:

      extension Range: Codable where Bound: Codable {
      
          private enum CodingKeys: CodingKey {
              case lowerBound
              case upperBound
          }
      
          public init(from decoder: Decoder) throws {
      
              let container = try decoder.container(keyedBy: CodingKeys.self)
      
              let lowerBound = try container.decode(Bound.self, forKey: .lowerBound)
              let upperBound = try container.decode(Bound.self, forKey: .upperBound)
      
              self.init(uncheckedBounds: (lower: lowerBound, upper: upperBound))
          }
      
          public func encode(to encoder: Encoder) throws {
      
              var container = encoder.container(keyedBy: CodingKeys.self)
      
              try container.encode(self.lowerBound, forKey: .lowerBound)
              try container.encode(self.upperBound, forKey: .upperBound)
          }
      }
      

      I think this conformance should be provided by the standard library.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              aciid Ankit Aggarwal
            • Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: