title: How do You Convert an AnyKeyPath to a WritableKeyPath?
body: <p>I have an array of enum cases, where each case has a <code>keyPath</code> property, which returns an <code>AnyKeyPath</code> matching the classes property with the same name as the enum case:</p>
  
  <pre><code>protocol PathAccessor: CodingKey {
      var keyPath: AnyKeyPath { get }
      static var allCases: [Self] { get }
  
      init?(rawValue: Int)
  }
  
  extension PathAccessor {
      static var allCases: [Self] {
          var cases: [Self] = []
          var index: Int = 0
          while let element = Self.init(rawValue: index) {
              cases.append(element)
              index += 1
          }
  
          return cases
      }
  }
  
  class Robot {
  
      let name: String
      var age: Int
      var powered: Bool
      var hasItch: Bool?
  
      enum CodingKeys: Int, PathAccessor {
          case name
          case age
          case powered
          case hasItch
  
          var keyPath: AnyKeyPath {
              switch self {
              case .name: return \Robot.name
              case .age: return \Robot.age
              case .powered: return \Robot.powered
              case .hasItch: return \Robot.hasItch
              }
          }
      }
  
      init(name: String, age: Int, powered: Bool) {
          self.name = name
          self.age = age
          self.powered = powered
      }
  }
  
  for element in Robot.CodingKeys.allCases {
      // Trying to implement
  }
  </code></pre>
  
  <p>In the loop above, I want to check the <code>keyPath</code> property of the case to see if it is a <code>WritableKeyPath</code>, and if it is, create a closure that will modify the property that the key path accesses.</p>
  
  <p>The problem with this is that a <code>WritableKeyPath</code> is a generic type. I know the <code>Root</code> type, but the <code>Value</code> type could be almost any type in existence. I could create a bunch of cases for each of most likely types:</p>
  
  <pre><code>if let path = element.keyPath as? WritableKeyPath&lt;Robot, Int&gt; {
  
  } else if let path = element.keyPath as? WritableKeyPath&lt;Robot, String&gt; {
  
  } // So on and so forth
  </code></pre>
  
  <p>But that is time consuming, ugly, and hard to maintain.</p>
  
  <p>I did try to cast to a dynamic type, but that gives a compiler error (<code>Use of undeclared type 'valueType'</code>):</p>
  
  <pre><code>let valueType = type(of: element.keyPath).valueType
  guard let path = element.keyPath as? WritableKeyPath&lt;Self, valueType&gt; else {
      continue
  }
  </code></pre>
  
  <p>I could use a protocol that the types already conform to, but for some reason, that is also failing:</p>
  
  <pre><code>guard let path = element.keyPath as? WritableKeyPath&lt;Robot, NodeInitializable&gt; else {
      print("bad")
      continue
  }
  print("good")
  
  // Output:
  // bad
  // bad
  // bad
  // bad
  </code></pre>
  
  <p>So, is it even <em>possible</em> to convert an <code>AnyKeyPath</code> to a <code>WritableKeyPath</code> without a huge string of unwrapping statements or weird hacks that shouldn't be used in production? </p>