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

Generated CodingKeys type is not available in function signatures

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode Version 9.0 beta (9M136h)
      Apple Swift version 4.0 (swiftlang-900.0.43 clang-900.0.22.8)

      Description

      Referencing the generated CodingKeys enum compiles just fine within a method body. For example:

      struct Working : Codable {
          let prop: String
          
          func blah() {
              let key: Working.CodingKeys = .prop
              print(key)
          }
      }
      

      However, if you try to include the CodingKeys type in a function signature, you get a compiler error saying 'CodingKeys' is not a member type of 'Broken'. For example, the following code doesn't compile:

      struct Broken : Codable {
          let prop: String
          
          func blah(key: Broken.CodingKeys) {
          }
      }
      

      You can work around the compile error by explicitly declaring the CodingKeys enum like so:

      struct Broken : Codable {
          let prop: String
          
          enum CodingKeys: String, CodingKey {
              case prop
          }
          func blah(key: Broken.CodingKeys) {
          }
      }
      

        Attachments

          Activity

            People

            • Assignee:
              itaiferber Itai Ferber
              Reporter:
              edwardaux Craig Edwards
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: